我的问题与this question非常相似。我使用ACTION_OPEN_DOCUMENT_TREE获得了一个目录的内容URI。
content://com.android.externalstorage.documents/tree/primary%3ASHAREit%2Fpictures
作为选择目录的结果。现在,我的问题是我如何访问目录内的所有文件(最好是子目录)。
我的问题与this question非常相似。我使用ACTION_OPEN_DOCUMENT_TREE获得了一个目录的内容URI。
content://com.android.externalstorage.documents/tree/primary%3ASHAREit%2Fpictures
作为选择目录的结果。现在,我的问题是我如何访问目录内的所有文件(最好是子目录)。
3条答案
按热度按时间svmlkihl1#
使用
DocumentFile.fromTreeUri()
为您的树创建aDocumentFile
。然后,使用listFiles()
获取该树中的文档和子树的列表。对于isDirectory()
返回true
的文档和子树,您可以进一步遍历树。对于其余的文档和子树,使用getUri()
获取Uri
。如果需要的话,您可以在ContentResolver
上使用openInputStream()
来获取内容。nwo49xxi2#
我是Android开发的新手,在很长一段时间里一直在理解CommonsWare的答案(以及其他资源)。
1.您需要DocumentFile AndroidX library,因此将其添加到您的
build.gradle
:1.获得包含
tree
的内容URI后,可以使用DocumentFile.fromTreeUri
现在我开始弄清楚如何使用这个DocumentFile...(提示:(一个月三个月一次)
xpszyzbs3#
下面是与@CommonsWare answer相关的代码片段。
使用
Intent.ACTION_OPEN_DOCUMENT_TREE
启动文件选取器从文件选取器接收所选目录的
Uri
使用
AsyncTask
复制文件(随意使用线程、协程..)