- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用的是包磊给出的代码,名称为 私有(private)乐趣保存图片在 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)
saveImage(bitmapImg, this.applicationContext)
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]]
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"
}
}
implementation "com.android.support:multidex:2.0.1"
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()
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
最佳答案
首先,您至少需要使用 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 开始可用。
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_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)
}
关于android - 找不到类 "android.provider.MediaStore$Downloads",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60374202/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!