我正在使用宝磊给出的名为的代码private fun save在how to save bitmap to android gallery上保存图像以保存位图
为了解决Kotlin中已过时的问题,我删除了以下几行:
//values.put(MediaStore.Images.Media.DATA, file.absolutePath)
//context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
并添加以下内容:
val contentValues = ContentValues()
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, folderName)
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
然后,我将该函数(在DidtodayActivity中)称为:
saveImage(bitmapImg, this.applicationContext)
该图像已下载到手机> Android>数据> com.example.MyApp1> files> MyFiles 。
我可以在设备上看到下载的图像。
但是,由于此行上的运行时错误, Activity 文件突然关闭(并且activity_main.xml直接打开):
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
原因:
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]]
详细消息:
**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]]
无法解决以下问题: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 {
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"
}
}
另外,以下代码已添加到build.gradle文件的依赖项部分中:
implementation "com.android.support:multidex:2.0.1"
并且,我在 list 文件中添加了以下代码:
android:name="androidx.multidex.MultiDexApplication">
如何解决运行时错误问题?
我试过了
this.applicationContext.contentResolver.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
代替这个;
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
但是,它再次因运行时错误而崩溃;
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()
但是, list 文件中已经包含以下代码;
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
因此,除了 MediaStore.downloads 之外,还有其他替代方法吗?
最佳答案
首先,您至少需要使用targetSdkVersion 29
https://developer.android.com/training/data-storage/shared/media
Downloaded files, which are stored in the Download/ directory. On devices that run
Android 10 (API level 29) and higher, these files are stored in the MediaStore.
Downloads table. **This table isn't available on Android 9 (API level 28) and lower.**
根据文档,此api从Android 10 api 29开始可用。在api 29以下,您必须使用
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
使用旧的方法将文件保存在Downloads文件夹中完整的解决方案:
private val FOLDER = "MyFolder"
private fun saveInDownloads(appContext: Context, fromFile: File) {
val dst = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
saveInDownloadsForQ(appContext, fromFile.name)
} else {
//dont forget to check if you have the permission
//to WRITE_EXTERNAL_STORAGE, and ask for it if you don't
saveInDownloadsBelowQ(appContext, fromFile.name)
}
dst?.let {
try {
val src = FileInputStream(fromFile)
dst.channel.transferFrom(src.channel, 0, src.channel.size())
src.close()
dst.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
private fun saveInDownloadsForQ(appContext: Context, fileName: String): FileOutputStream? {
val contentValues = ContentValues().apply {
put(MediaStore.Downloads.DISPLAY_NAME, fileName)
put(MediaStore.Downloads.MIME_TYPE, "application/pdf")
put(MediaStore.Downloads.DATE_ADDED, (System.currentTimeMillis() / 1000).toInt())
put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + File.separator + FOLDER)
}
val resolver = appContext.contentResolver
val uri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
return uri?.let {
resolver.openOutputStream(uri) as FileOutputStream
}
}
private fun saveInDownloadsBelowQ(appContext: Context, fileName: String): FileOutputStream {
val path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).toString()
val dir = File(path, FOLDER)
val dirFlag = dir.mkdirs()
val file = File(dir, fileName)
return FileOutputStream(file)
}