【问题标题】:How to get audio files URIs having directory URI?如何获取具有目录 URI 的音频文件 URI?
【发布时间】:2019-05-13 20:06:50
【问题描述】:

用户选择目录,我需要获取该目录中的所有音频文件 URI。
我有类似这样的目录 URI:

content://com.android.providers.downloads.documents/tree/downloads

当我尝试使用 ContentResolver 方法查询时出现错误:

Failure delivering result ResultInfo{who=null, request=9999, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/tree/downloads flg=0xc3 }} to activity {com.example.directorypicker/com.example.directorypicker.MainActivity}: java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.providers.downloads.documents/tree/downloads

如何使 URI 支持?
也许你有其他选择?

【问题讨论】:

  • @pskink 是的,但我用Intent.ACTION_OPEN_DOCUMENT_TREE 创建了我的意图,我的路径总是有/tree/someDir。当我剪切/下载时,我得到了 ACTION_OPEN_DOCUMENT 要求
  • ACTION_OPEN_DOCUMENT_TREE 的文档说:“要访问后代(子、孙等)文档,请使用 buildDocumentUriUsingTree(Uri, String) 和 buildChildDocumentsUriUsingTree(Uri, String)返回的 URI。” - 你试过了吗?
  • @pskink 如果我理解正确,此文件夹中的文件已编入索引(索引是第二个参数)。但我不知道如何获得这些字符串。尝试 (programcreek.com/java-api-examples/…) 和 DocumentsContract.getDocumentIdInvalid URI: content://com.android.providers.downloads.documents/tree/downloads 崩溃。我认为那个例子中的 param self 就像我的目录 URI,但是出了点问题。

标签: android android-contentprovider


【解决方案1】:

获取从ACTION_OPEN_DOCUMENT_TREE 获得的Uri 并将其传递给DocumentFile.fromTreeUri()。这将为您提供一个DocumentFile 对象,您可以调用listFiles() 来遍历文档树以查找您的音频内容。

【讨论】:

    猜你喜欢
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2014-06-06
    • 2018-02-24
    相关资源
    最近更新 更多