Java 在不使用findFiles()的情况下获取文档树的子文档文件

Java 在不使用findFiles()的情况下获取文档树的子文档文件,java,android,storage-access-framework,documentfile,Java,Android,Storage Access Framework,Documentfile,我有一个从ACTION\u OPEN\u DOCUMENT\u tree获取的树URI,如果不使用findFiles()?假设我知道如何获取它的documentId、绝对路径或URI。我需要与文档树关联的权限 这就是我现在拥有的: DocumentFile rootTree = DocumentFile.fromTreeUri(context, rootTreeUri); DocumentFile docFile = rootTree.findFile(fileName); 它返回docFil

我有一个从
ACTION\u OPEN\u DOCUMENT\u tree
获取的树URI,如果不使用
findFiles()?假设我知道如何获取它的documentId、绝对路径或URI。我需要与文档树关联的权限

这就是我现在拥有的:

DocumentFile rootTree = DocumentFile.fromTreeUri(context, rootTreeUri);
DocumentFile docFile = rootTree.findFile(fileName);
它返回
docFile
作为我根目录的子目录TreeDocumentFile。我有写入权限,可以对其使用
createFile()
,因为它位于
ACTION\u OPEN\u document\u tree
的文档树下

所以它可以工作,但是
findFile()
非常慢

如果我尝试使用文档,请这样做:

// 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"

它返回一个以
docFile
为根的新TreeDocumentFile,而不是
docFile
作为原始文档树(root)的子级。所以我没有写这棵树的权限

如果我这样做:

Uri docUri = DocumentsContract.buildDocumentUriUsingTree(rootTreeUri, documentId);
// Result: "content://com.android.externalstorage.documents/tree/1A0E-0E2E:/document/1A0E-0E2E:myChildDirectory"

我得到了一个看起来像我想要的URI,但它是一个URI,而不是一个文档文件

如果我执行与上面相同的操作,但使用fromTreeUri()从此uri构建文档文件:


我得到的是原始的树文档文件,而不是表示子级的文档文件。

当前API无法满足您的需要。获取make a(支持
createFile
createDirectory
listFiles
、和
renameTo
)的
DocumentFile.fromTreeUri
的私有子类)的唯一方法是通过
DocumentFile.fromTreeUri
(它只提供您找到的根树URI)或者通过现有的
treedDocumentFile
listFiles()
方法,这是
findFile
在内部使用的方法


您应该在上提交一个功能请求,以添加一个新的静态方法,该方法可以满足您的需要。

“我得到一个URI,它实际上看起来像我想要的,但它是一个URI,而不是一个文档文件”--使用
fromsingleri()
打开
DocumentFile
将其包装在
DocumentFile
中。我没有这样创建的
DocumentFile
的写入权限,不能使用
createFile()
。这很奇怪。好吧,你应该拥有整棵树的权利。在封面下,
DocumentFile
只是使用
DocumentsContract
来做这类事情——这不像
DocumentFile
有你没有的特殊访问权限。@commonware-由
fromsingeluri
创建的
SingleDocumentFile
没有实现
createFile
(在其他方法中),即使是从使用
buildDocumentUriUsingTree
构建的URI创建的,我明白了。谢谢你的回答。我很高兴谷歌修复了它,并使访问外部sd卡变得更好。这应该在androidx.documentfile:1.0.1中解决-
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(rootTreeUri, documentId);
DocumentFile docFile = DocumentFile.fromTreeUri(context, docUri);
// Result: "content://com.android.externalstorage.documents/tree/1A0E-0E2E:/document/1A0E-0E2E:"