如何使用Action_View打开“授予URI”的子文件?
上下文:
简单的文件资源管理器显示文件夹内容,并允许用户根据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_storage
和write_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
andWRITE_EXTERNAL_STORAGE
I can also delete the child document by using
childDocument.delete()
it works fine, but when I try to launch it onACTION_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论