【问题标题】:Android 10 MediaStore update image RecoverableSecurityExceptionAndroid 10 MediaStore 更新图像 RecoverableSecurityException
【发布时间】:2020-01-07 05:45:56
【问题描述】:

我正在将 Android 应用更新为范围存储,我现在面临的问题是,在我的应用上,用户可以选择一组要自动处理的图像(他们可以选择覆盖原始图像)图片)。

问题是,当我试图从其 Uri 覆盖原始图像时,Android 会抛出一个RecoverableSecurityException,它会向用户显示一个对话框,请求同意修改该照片。

我想知道的是,是否可以改为显示权限对话框,但可以同时显示整组图像,例如 10 张图像,而不必为每张图像显示一个对话框。

我尝试使用ContentResolverapplyBatch 方法(下面的代码模拟了问题),但同意对话框仅显示第一个更新的图像。

val values = ContentValues().apply {
                put(MediaStore.Images.Media.DATE_MODIFIED, System.currentTimeMillis())
            }
            val ops = arrayListOf<ContentProviderOperation>()
            for (image in galleryImages) {
                ops.add(ContentProviderOperation.newUpdate(image.uri)
                        .withValues(values)
                        .build())
            }
            context.contentResolver.applyBatch(MediaStore.AUTHORITY, ops)

对此有什么想法吗?谢谢。

【问题讨论】:

    标签: android mediastore android-10.0 scoped-storage


    【解决方案1】:

    不幸的是,在 Android Q 中是不可能的。但是,在 2019 年 Android 峰会上,该团队表示在下一个 Android 版本中可以进行批量编辑和删除,因此应该是在未来的“R”版本中。

    观看下一个视频,从 18:50 开始,他们简要地谈论它:

    https://youtu.be/UnJ3amzJM94

    Android Q 中唯一的解决方案是获取您要操作的目录的 SAF 权限,并使用 DocuentFile API 而不是 MediaStore。

    【讨论】:

      【解决方案2】:

      另外,对于 android 11,我找到了一种无需额外权限请求即可通过捕获 RecoverableSecurityException 并抛出我自己的异常来解决它的方法:

      try {
          //try to modify
      } catch (RecoverableSecurityException e) {
          List<Uri> uris = //map photo list to uri
          PendingIntent pIntent = MediaStore.createWriteRequest(contentResolver, uris);
          throw new RecoverableSecurityExceptionExt(pIntent);
      }
      
      
      public class RecoverableSecurityExceptionExt extends SecurityException {
      
          private final PendingIntent pIntent;
      
          public RecoverableSecurityExceptionExt(PendingIntent pIntent) {
              this.pIntent = pIntent;
          }
      
          public PendingIntent getPIntent() {
              return pIntent;
          }
      }
      

      然后捕获第二个异常,从 PendingIntent 获取 IntentSender 并像常规 RecoverableSecurityException 一样使用 startIntentSenderForResult(...) 等处理它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-25
        • 1970-01-01
        • 2016-08-29
        • 1970-01-01
        相关资源
        最近更新 更多