如何用适合 google 控制台的内容替换 requestLegacyExternalStorage 来复制用户文件?
How can replace requestLegacyExternalStorage with something suitable for google console for copying user file?
我正在尝试从用户存储中复制图像
File(userStorageImagePath).copyTo(userProfileImage, overwrite = true)
但用户选择图像时出现异常:
open failed: EACCES (Permission denied) /storage/emulated/0/Download/....
授予所有访问权限 read/write 和例外
仅出现在 android 10
android:requestLegacyExternalStorage="true"
解决问题,
但是如何将 requestLegacyExternalStorage 替换为适合 google 的内容?
已解决:
val inputStream: InputStream? = getMediaUriFromPath(ctx, userStorageImagePath)?.let { ctx.contentResolver.openInputStream(it) }
Files.copy(inputStream, imageDestinationPath, StandardCopyOption.REPLACE_EXISTING)
fun getMediaUriFromPath(context: Context, path: String): Uri? {
val mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val cursor: Cursor? = context.contentResolver.query(
mediaUri,
null,
MediaStore.Images.Media.DISPLAY_NAME + "= ?",
arrayOf(path.substring(path.lastIndexOf("/") + 1)),
null
)
var uri: Uri? = null
if (cursor != null) {
if (cursor.moveToFirst()) {
uri = ContentUris.withAppendedId(
mediaUri,
cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID))
)
}
}
cursor?.close()
return uri
}
我正在尝试从用户存储中复制图像
File(userStorageImagePath).copyTo(userProfileImage, overwrite = true)
但用户选择图像时出现异常:
open failed: EACCES (Permission denied) /storage/emulated/0/Download/....
授予所有访问权限 read/write 和例外 仅出现在 android 10
android:requestLegacyExternalStorage="true"
解决问题,
但是如何将 requestLegacyExternalStorage 替换为适合 google 的内容?
已解决:
val inputStream: InputStream? = getMediaUriFromPath(ctx, userStorageImagePath)?.let { ctx.contentResolver.openInputStream(it) }
Files.copy(inputStream, imageDestinationPath, StandardCopyOption.REPLACE_EXISTING)
fun getMediaUriFromPath(context: Context, path: String): Uri? {
val mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val cursor: Cursor? = context.contentResolver.query(
mediaUri,
null,
MediaStore.Images.Media.DISPLAY_NAME + "= ?",
arrayOf(path.substring(path.lastIndexOf("/") + 1)),
null
)
var uri: Uri? = null
if (cursor != null) {
if (cursor.moveToFirst()) {
uri = ContentUris.withAppendedId(
mediaUri,
cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID))
)
}
}
cursor?.close()
return uri
}