我试图从具有特定 MIME 类型的目录中获取所有文件 - 我想要所有图像。
我使用了一些示例代码,其中使用 MediaStore 作为 URI,但后来发现很难为所选目录过滤它,因为结果集中返回的 URI 的格式与我提供的 URI 的格式不同...
所以我找到了这个示例代码https://github.com/googlesamples/android-DirectorySelection https://github.com/googlesamples/android-DirectorySelection
它查询所选子树上的 DocumentContract,现在需要过滤所需的 MIME 类型。
问题是:无论我提供什么作为选择参数,它总是会列出该目录中找到的所有文件/目录。
我什至尝试“1=2”作为选择,这仍然列出了所有内容。
任何想法我做错了什么?
val ChildrenUri = 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)
}