I am using codes given by Bao Lei with the name private fun saveImage at how to save bitmap to android gallery to save bitmap
To solve deprecated problems in Kotlin, I removed these lines:
//values.put(MediaStore.Images.Media.DATA, file.absolutePath)
//context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
and added this:
val contentValues = ContentValues()
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, folderName)
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
Then, I called the function (in DidtodayActivity ) as:
saveImage(bitmapImg, this.applicationContext)
The image is downloaded to Phone>Android>Data>com.example.MyApp1>files>MyFiles. I can see the downloaded image in my device.
But, the activity file is closed suddenly (and activity_main.xml is opened directly) due to the runTime error at this line:
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
Cause:
java.lang.ClassNotFoundException: Didn't find class "android.provider.MediaStore$Downloads" on path: DexPathList[[zip file "/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/lib/arm64, /data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk!/lib/arm64-v8a, /system/lib64]]
Detailmessage:
**Didn't find class "android.provider.MediaStore$Downloads"** on path: DexPathList[[zip file "/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/lib/arm64, /data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk!/lib/arm64-v8a, /system/lib64]]
Failed resolution of: Landroid/provider/MediaStore$Downloads;
Module level build.gradle is:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.MyApp1"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
aaptOptions{
noCompress "tflite"
noCompress "lite"
}
}
Also, the following code was added into the dependencies part in the build.gradle file:
implementation "com.android.support:multidex:2.0.1"
And, I added this code in the Manifest file:
android:name="androidx.multidex.MultiDexApplication">
How to solve the run time error problem ?
I tried this;
this.applicationContext.contentResolver.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
instead of this;
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
But, it crashes again with this runtime error;
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=203, result=-1, data=Intent { (has extras) }} to
activity {com.example.MyApp1/com.example.MyApp1.DidtodayActivity}: java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri
content://media/external/images/media from pid=3812, uid=10270
requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
However, the following code is already in the Manifest file;
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
So, is there any other alternative to use instead of MediaStore.Downloads?