RecoverableSecurityException导致删除文件失败。
在Android Q中,使用MediaStore getContentResolver()。query()获取光标,然后使用while循环获取photoUri,使用cursor.moveToNext()组合将photoUri添加到Arraylist中。 文件路径代码如下:
Uri photoUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media._ID)));
然后,使用:
mContext.getContentResolver().delete(photoUri, null, null);
始终执行RecoverableSecurityException。我知道有一种弹出对话框的方式,可以将其删除。
((Activity)mContext).startIntentSenderForResult(e1.getUserAction().getActionIntent().getIntentSender(), 100, null, 0, 0, 0);
具有删除文件跳过异常的任何方法。或其他删除文件的方式。文件路径存储在外部存储中,如下所示:pictures / abc.jpg
答案 0 :(得分:0)
您应该获得访问Android Q中存储空间的权限。
我通过使用此权限请求解决了
if (DEVICE_VERSION >= 29) {
//NOTE: This method and its functions compatible with API Level 24 and above but I am using for only 29 and above
List<UriPermission> permissions = MainActivity.this.getContentResolver().getPersistedUriPermissions();
if (permissions.isEmpty()) {
StorageManager manager = getSystemService(StorageManager.class);
StorageVolume primaryStorageVolume = manager.getPrimaryStorageVolume();
//For open the permission allowing page
startActivityForResult(primaryStorageVolume.createOpenDocumentTreeIntent(), STORAGE_PERMISSION_REQ_CODE);
} else {
for (UriPermission up : permissions) {
String permissionUri = up.getUri().toString();
if (permissionUri.contains("DCIM") || permissionUri.endsWith("%3A") || permissionUri.endsWith("A")) {
requestExternalStoragePermission(); //I also used this method for reading and writing of external storage.
return;
}
}
}
}
然后,在onActivityResult()方法中,应使用STORAGE_PERMISSION_REQ_CODE进行检查,并使用takePersistableUriPermission()进行设置:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == STORAGE_PERMISSION_REQ_CODE) {
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
MainActivity.this.getContentResolver().takePersistableUriPermission(data.getData(), takeFlags);
}
}
它适合我的情况。