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