我正在尝试从具有特定 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 不支持子项的选择或排序参数,它已损坏。
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/