android - DocumentContract 上的 ContentResolver 查询列出所有文件,忽略选择

标签 android android-contentresolver

我正在尝试从具有特定 MIME 类型的目录中获取所有文件 - 我想要所有图像。

我使用了一些示例代码,其中使用 MediaStore 作为 URI,但后来发现很难为所选目录过滤它,因为结果集中返回的 URI 的格式与我提供的 URI 的格式不同...

所以我找到了这个示例代码 https://github.com/googlesamples/android-DirectorySelection

它查询所选子树上的 DocumentContract,现在需要过滤所需的 MIME 类型。

问题是:无论我提供什么作为选择参数,它总是会列出该目录中找到的所有文件/目录。

我什至尝试过“1=2”作为选择,但这仍然列出了所有内容。 有什么想法我做错了吗?

valchildrenUri = DocumentsContract.buildChildDocumentsUriUsingTree( 乌里, DocumentsContract.getTreeDocumentId(uri) )

        val childCursor = contentResolver.query(
            childrenUri,
            arrayOf(DocumentsContract.Document.COLUMN_DISPLAY_NAME, COLUMN_MIME_TYPE),
            "$COLUMN_MIME_TYPE=?",
            Array(1){MimeTypeMap.getSingleton().getExtensionFromMimeType("jpg")},
            null
        )
        Log.i("ADDFOLDER", "files: ${childCursor.count}")
        try {
            while (childCursor.moveToNext()) {
                Log.d(
                    TAG, "found child=" + childCursor.getString(0) + ", mime=" + childCursor
                        .getString(1)
                )
            }
        } finally {
            closeQuietly(childCursor)
        }

最佳答案

FileSystemProvider 不支持子项的选择或排序参数,它已损坏。

https://github.com/aosp-mirror/platform_frameworks_base/blob/53a9ccaa926945149b4546c67b50ce1ae88ba777/core/java/com/android/internal/content/FileSystemProvider.java#L285

The base DocumentsProvider strips the selection args for a children query as well, so I wouldn't rely on it ever working. You could use the search documents Uri, which does do filtering, but still ignores the sort order (Edit: and more importantly, is a recursive search, so it'll search within any subfolders.)*

*来自:https://www.reddit.com/r/androiddev/comments/b80qqt/weekly_questions_thread_april_01_2019/ek9oew6/

关于android - DocumentContract 上的 ContentResolver 查询列出所有文件,忽略选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56263620/

相关文章:

android - 在启动时将所有静态缓冲区加载到显存?

java - 获取 child 的名字以及 Firebase 数据库中的值

android - 如何在android中以编程方式删除联系人

android - 如何在 Android 10 Scoped Storage 中删除图像(Mediastore 条目和文件)

android - 使用 applyBatch 插入数千个联系人条目很慢

android - (Android) - 当用户到达某个位置时如何触发警报?

android - hdpi 资源不再加载

Android 选项菜单图标不会显示

android - 指定为非null的参数为null:参数投影Content Provider Kotlin

Android 优化 ContentResolver 查询