我正在开发的应用程序使用File作为拍摄照片的目标。这是通过用户使用Intent(ACTION_IMAGE_CAPTURE)选择相机应用程序在外部执行的。自从将build和target-sdk升级到29版本以来,已经出现了一系列问题,首先是对自由访问外部存储上的文件的限制。第一个更改是使用应用程序的私有缓存目录之一,例如:
File.createTempFile("tempImage", ".jpg", context.cacheDir)
或应用程序私有外部存储目录:
File.createTempFile("tempImage", ".jpg", context.getExternalFilesDir(Environment.DIRECTORY_PICTURES))
与file_paths.xml中的FileProvider access结合使用,例如:
<paths>
<external-path name="images" path="Pictures/" /><!-- prior to SDK-29, use public external storage -->
<external-files-path name="externalImages" path="Pictures/" />
<files-path name="internalImages" path="internalImages/"/>
<cache-path name="cache" path="/" />
</paths>在正确配置后,这些功能现在可以很好地工作,但是实现“保存到图库”功能,例如:通知其他应用程序新图像在运行Android-10的设备上不再起作用
// use FileProvider to make this new photo publicly accessible
val shareableUri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, newImage)
context.sendBroadcast(
Intent(ACTION_MEDIA_SCANNER_SCAN_FILE).apply { data = uris.externalUri }
)这种方法应该有效,但不管原始图像保存在哪里(private-app-dir、cache-dir、external-private)
MediaScannerConnection.scanFile(context, arrayOf(newImage.absolutePath), arrayOf("image/jpeg")) { path: String, uri: Uri? ->
if (uri == null) {
throw IllegalStateException("media scan failed...")
} else {
// successful
}
}在Android的SDK-29中是否有新的限制,需要在MediaScanning中进行更改,特别是与扫描(可能)私人图像文件的方式相关?我注意到MediaScanner方法大多需要String路径而不是URI,因此我认为新的限制并不适用于它,因为它是一个系统组件。
发布于 2019-09-17 15:09:08
默认情况下,针对Android 10 (API级别29)和更高版本的应用程序被授予对外部存储设备或范围存储的作用域访问权限。
所以你需要让它与scopedStorage兼容,但你可以暂时使用Google Android开发人员指南中提到的这种临时方法。
在你的应用完全兼容作用域存储之前,你可以根据你的应用的目标SDK级别或requestLegacyExternalStorage清单属性临时选择退出:
<manifest ... >
<!-- This attribute is "false" by default on apps targeting
Android 10 or higher. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
有关更多信息,请访问以下链接:https://developer.android.com/training/data-storage/files/external-scoped
https://stackoverflow.com/questions/57963282
复制相似问题