SecurityException:从 ACTION_IMAGE_CAPTURE 中找不到 uri 的持久权限授予

Posted

技术标签:

【中文标题】SecurityException:从 ACTION_IMAGE_CAPTURE 中找不到 uri 的持久权限授予【英文标题】:SecurityException: No persistable permission grants found for uri from ACTION_IMAGE_CAPTURE 【发布时间】:2016-11-26 19:10:36 【问题描述】:

我的应用使用相机拍照并长期使用。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri resultUri = null;
resultUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
      new ContentValues());
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, resultUri);
startActivityForResult(imageCaptureIntent, IMAGE_CAPTURE_REQUEST_CODE);

但是当我调用 takePersistableUriPermission() 时,我会得到 SecurityException: No persistable permission grants found

我读过这个 Getting Permission Denial Exception。 它非常适合 ACTION_OPEN_DOCUMENT。 如何获得相机的持久许可?

【问题讨论】:

你有 URI - 为什么你需要持久访问? @ianhanniballake 哇。你说得对。重新启动后,无需持久许可即可访问来自摄像头的 uri。但是,如果我不调用 takePersistableUriPermission(),为什么它无法从 ACTION_GET_CONTENT 获取 uri? 【参考方案1】:

访问MediaStore URIs 只受存储权限控制(即READ_EXTERNAL_STORAGE)所以只要你继续持有存储权限,你就可以访问Uris 所以在这种情况下你不需要完全保留权限。

URI based permissions,用于ACTION_GET_CONTENTACTION_OPEN_DOCUMENT 等。通过包含在返回的Intent 中的FLAG_GRANT_READ_URI_PERMISSION 提供对URI 的特殊一次性访问。

只有文档 URI(DocumentsContract.isDocumentUri() 返回 true 的那些)允许您通过 persist permissions 提供对 Uri 的更永久访问权限。

【讨论】:

感谢您的解释。这很有帮助。 不完全是。如果您要求用户使用 Intent.ACTION_GET_CONTENT 选择文件 - 您收到的 URI 将成功通过 DocumentsContract.isDocumentUri() 但如果您尝试保存权限调用 takePersistableUriPermission() 将失败,但 SecurityException: No persistable permission grants found for //UID 10829 and Uri content://... 除外;您应该使用Intent.ACTION_OPEN_DOCUMENT 打开文件选择器。

以上是关于SecurityException:从 ACTION_IMAGE_CAPTURE 中找不到 uri 的持久权限授予的主要内容,如果未能解决你的问题,请参考以下文章

EclipseLink2.7出现java.lang.SecurityException异常

如何在运行时检查权限而不抛出 SecurityException?

在沙盒 Appdomain 中加载程序集 - SecurityException

授予 FileProvider 的 uri 权限会产生 SecurityException

Android 9.0 NotificationManager.notify() 抛出 java.lang.SecurityException

在 Android 10 上安装更新同一应用的 apk 失败; java.lang.SecurityException:文件仍然打开