我有一个从中获得的树 URI ACTION_OPEN_DOCUMENT_TREE
,如何在不使用的情况下获得作为该树的子项的 DocumentFilefindFiles()
?鉴于我知道如何获取它的 documentId、它的绝对路径或它的 URI。我需要与文档树相关的权限。
这就是我现在所拥有的:
DocumentFile rootTree = DocumentFile.fromTreeUri(context, rootTreeUri);
DocumentFile docFile = rootTree.findFile(fileName);
它docFile
以 TreeDocumentFile 的形式返回,它是我的根目录的子项。我有写权限并且可以使用createFile()
它,因为它位于ACTION_OPEN_DOCUMENT_TREE
.
所以它有效,但findFile()
真的很慢。
如果我尝试像这样使用 DocumentsContract:
// I know how to build the documentId from the file's absolute path
Uri uri = DocumentsContract.buildTreeDocumentUri(rootTreeUri.getAuthority(), documentId);
DocumentFile docFile = DocumentFile.fromTreeUri(context, uri);
// Result: "content://com.android.externalstorage.documents/tree/1A0E-0E2E:myChildDirectory/document/1A0E-0E2E:myChildDirectory"
它返回一个以 为根的新 TreeDocumentFile docFile
,而不是docFile
作为我的原始文档树(根)的子级。所以我没有这棵树的写权限。
如果我这样尝试:
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(rootTreeUri, documentId);
// Result: "content://com.android.externalstorage.documents/tree/1A0E-0E2E:/document/1A0E-0E2E:myChildDirectory"
我得到一个实际上看起来像我想要的 URI,但它是一个 URI,而不是 DocumentFile。
如果我执行与上述相同的操作,但使用 fromTreeUri() 从该 uri 构建 DocumentFile:
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(rootTreeUri, documentId);
DocumentFile docFile = DocumentFile.fromTreeUri(context, docUri);
// Result: "content://com.android.externalstorage.documents/tree/1A0E-0E2E:/document/1A0E-0E2E:"
我得到了原始树 DocumentFile,而不是代表孩子的 DocumentFile。