如何使用Action_View打开“授予URI”的子文件?

发布于 2025-01-23 21:47:01 字数 2587 浏览 0 评论 0原文

上下文:

简单的文件资源管理器显示文件夹内容,并允许用户根据MIME类型的

预期行为打开任何文件:

  • 的按钮中
  • 单击启动open_document_tree活动用户
  • 显示所有文件夹在列表视图中的内容
  • 当用户单击列表项目中时,启动action_view在另一个支持支持渲染的应用程序中打开文件文档内容

我实际上得到的内容:

  • 在最后一步中,securityException正在抛出

我的意图的形式:

fun someOfGrantedUris(): Uri {
  // These Uris were returned by `ACTION_OPEN_DOCUMENT_TREE` activity
  val grantedUris = contentResolver.persistedUriPermissions
  
  return grantedUris[0] // Consider this a Uri always not null and valid
}

val parentDocument = DocumentFile.fromTreeUri(someOfGrantedUris())

val childDocument = parentDocument.listFiles()[0] // First child document

val uri = childDocument.uri

val intent =
  Intent(Intent.ACTION_VIEW).apply {
    addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    addCategory(Intent.CATEGORY_DEFAULT)

    // This is the same of just `uriWithProviderScheme = uri`
    // but in this way I can change any part of the Uri to test and debug
    val uriWithProviderScheme = Uri.Builder().let {
      it.scheme(uri.scheme)
      it.path(uri.path)
      it.query(uri.query)
      it.authority(uri.authority)
      it.build()
    }

  setDataAndType(uriWithProviderScheme, type)
}

但是当我尝试启动此活动时:

try {
  // This is not native Android but a Flutter plugin
  plugin.binding?.activity?.startActivity(intent, null)
} catch (e: SecurityException) {
  // I always fall here
}
  • 我已经获得了所有运行时的权限:read_external_storagewrite_external_storage

  • 我也可以使用childdocument.delete.delete.delete() IT删除儿童文档工作正常,但是当我尝试通过action_view抛出SecurityException时,为什么?

class java.lang.SecurityException: UID 10498 does not have permission to content://com.android.externalstorage.documents/tree/primary%3ADownload/offline/document/primary%3ADownload/offline/Sample%20File.txt [user 0]; you could obtain access using ACTION_OPEN_DOCUMENT or related APIs

即使我们可以尝试使用action_open_document这不是我想要的,因为我已经在使用action_open_document_tree和docs和docs:

授予访问 目录的目录action_open_document_tree意图操作,可在Android 5.0(API级别21及更高)上使用,允许用户允许用户要选择一个特定目录, 授予您对该目录中所有文件和子目录的访问。

Context:

Simple file explorer which shows a folder contents and allow user open any file with another app depending of the mime type

The expected behavior:

  • Click in a button that launch OPEN_DOCUMENT_TREE activity
  • User select a folder A
  • Display all folder A contents in a list view
  • When user click in a list item, launch ACTION_VIEW to open the file document inside another app that support rendering the contents

What I got actually:

  • In the last step, the SecurityException is being thrown

The generation of my intent looks like:

fun someOfGrantedUris(): Uri {
  // These Uris were returned by `ACTION_OPEN_DOCUMENT_TREE` activity
  val grantedUris = contentResolver.persistedUriPermissions
  
  return grantedUris[0] // Consider this a Uri always not null and valid
}

val parentDocument = DocumentFile.fromTreeUri(someOfGrantedUris())

val childDocument = parentDocument.listFiles()[0] // First child document

val uri = childDocument.uri

val intent =
  Intent(Intent.ACTION_VIEW).apply {
    addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    addCategory(Intent.CATEGORY_DEFAULT)

    // This is the same of just `uriWithProviderScheme = uri`
    // but in this way I can change any part of the Uri to test and debug
    val uriWithProviderScheme = Uri.Builder().let {
      it.scheme(uri.scheme)
      it.path(uri.path)
      it.query(uri.query)
      it.authority(uri.authority)
      it.build()
    }

  setDataAndType(uriWithProviderScheme, type)
}

But when I try to start this activity:

try {
  // This is not native Android but a Flutter plugin
  plugin.binding?.activity?.startActivity(intent, null)
} catch (e: SecurityException) {
  // I always fall here
}
  • I have all runtime permissions already granted: READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE

  • I can also delete the child document by using childDocument.delete() it works fine, but when I try to launch it on ACTION_VIEW a SecurityException is thrown, why?

class java.lang.SecurityException: UID 10498 does not have permission to content://com.android.externalstorage.documents/tree/primary%3ADownload/offline/document/primary%3ADownload/offline/Sample%20File.txt [user 0]; you could obtain access using ACTION_OPEN_DOCUMENT or related APIs

Even though we can try to use ACTION_OPEN_DOCUMENT this is not what I'm looking for since I already requested permissions before with ACTION_OPEN_DOCUMENT_TREE and by the docs:

Grant access to a directory's contents: The ACTION_OPEN_DOCUMENT_TREE intent action, available on Android 5.0 (API level 21) and higher, allows users to select a specific directory, granting your app access to all of the files and sub-directories within that directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文