【问题标题】:How can replace requestLegacyExternalStorage with something suitable for google console for copying user file?如何用适合谷歌控制台复制用户文件的东西替换 requestLegacyExternalStorage?
【发布时间】:2021-09-16 23:38:27
【问题描述】:

我正在尝试从用户存储中复制图像

File(userStorageImagePath).copyTo(userProfileImage, overwrite = true)

但随后用户选择了出现异常的图像:

open failed: EACCES (Permission denied)  /storage/emulated/0/Download/....

所有访问权限被授予读/写和异常 仅在 android 10 上出现

android:requestLegacyExternalStorage="true"解决问题,

但是如何将 requestLegacyExternalStorage 替换为适合 google 的内容?

【问题讨论】:

    标签: android exception permissions copyto


    【解决方案1】:

    已解决:

    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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 2018-04-17
      • 2019-09-23
      • 2016-12-18
      相关资源
      最近更新 更多