Android 10 MediaStore 更新图像 RecoverableSecurityException
Android 10 MediaStore update image RecoverableSecurityException
我正在将 Android 应用程序更新为范围存储,我现在面临的问题是在我的应用程序上,用户可以 select 一组要处理的图像自动(他们可以选择覆盖原始图像)。
问题是,当我试图从其 Uri 覆盖原始图像时,Android 抛出一个 RecoverableSecurityException
,它向用户显示一个对话框,请求同意修改该照片。
我想知道的是,是否可以改为显示一个权限对话框,但是对于整组图像,例如一次显示 10 张图像,而不是必须为每个图像显示一个对话框。
我尝试使用 ContentResolver
的 applyBatch
方法(下面的代码模拟了这个问题),但是同意对话框只显示第一个更新的图像。
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 Q 是不可能的。但是,在 Android 2019 年峰会上,团队表示在下一个 Android 版本中将可以进行批量编辑和删除,因此据说在未来的版本 'R'.
观看下一个视频,从第 18:50 分钟开始,他们会在其中简短地谈论它:
AndroidQ 中的唯一解决方案是获得对您要操作的目录的 SAF 权限,并使用 DocuentFileAPI 而不是 MediaStore。
另外,对于 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 并像使用 startIntentSenderForResult(...) 的常规 RecoverableSecurityException 一样处理它,依此类推
我正在将 Android 应用程序更新为范围存储,我现在面临的问题是在我的应用程序上,用户可以 select 一组要处理的图像自动(他们可以选择覆盖原始图像)。
问题是,当我试图从其 Uri 覆盖原始图像时,Android 抛出一个 RecoverableSecurityException
,它向用户显示一个对话框,请求同意修改该照片。
我想知道的是,是否可以改为显示一个权限对话框,但是对于整组图像,例如一次显示 10 张图像,而不是必须为每个图像显示一个对话框。
我尝试使用 ContentResolver
的 applyBatch
方法(下面的代码模拟了这个问题),但是同意对话框只显示第一个更新的图像。
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 Q 是不可能的。但是,在 Android 2019 年峰会上,团队表示在下一个 Android 版本中将可以进行批量编辑和删除,因此据说在未来的版本 'R'.
观看下一个视频,从第 18:50 分钟开始,他们会在其中简短地谈论它:
AndroidQ 中的唯一解决方案是获得对您要操作的目录的 SAF 权限,并使用 DocuentFileAPI 而不是 MediaStore。
另外,对于 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 并像使用 startIntentSenderForResult(...) 的常规 RecoverableSecurityException 一样处理它,依此类推