我正在使用鲍雷提供的代码,名称为private fun saveImage在如何将位图保存到 android gallery来保存位图
为了解决 Kotlin 中已弃用的问题,我删除了这些行:
//values.put(MediaStore.Images.Media.DATA, file.absolutePath)
//context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
Run Code Online (Sandbox Code Playgroud)
并添加了这个:
val contentValues = ContentValues()
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, folderName)
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
Run Code Online (Sandbox Code Playgroud)
然后,我将该函数(在 DidtodayActivity 中)称为:
saveImage(bitmapImg, this.applicationContext)
Run Code Online (Sandbox Code Playgroud)
图像被下载到Phone>Android>Data>com.example.MyApp1>files>MyFiles。我可以在我的设备中看到下载的图像。
但是,由于这一行的运行时错误,活动文件突然关闭(并且直接打开activity_main .xml):
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
Run Code Online (Sandbox Code Playgroud)
原因:
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]]
Run Code Online (Sandbox Code Playgroud)
详细信息:
**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]]
Run Code Online (Sandbox Code Playgroud)
解析失败:Landroid/provider/MediaStore$Downloads;
模块级别的build.gradle是:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
android { …Run Code Online (Sandbox Code Playgroud)