Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentResolver query on Documents not filtering with selection

I am trying to filter documents on display name. Here is the code:

val files = ArrayList<String>(0)
val uri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getTreeDocumentId(rootUri))
val searchQuery = "${DocumentsContract.Document.COLUMN_DISPLAY_NAME} LIKE ?"
val cursor = cResolver.query(uri, arrayOf(DocumentsContract.Document.COLUMN_DISPLAY_NAME), searchQuery, arrayOf("'%aaa%'"), null)
while (cursor?.moveToNext() == true){
    files.add(cursor.getString(0).toString())
}

Here I am getting the output as [ap.apk, mytext.txt, aaa]
Whereas the expected output is just [aaa]

I have looked into other questions.
Accourding to these questions:
where clause in contentProvider's query in Android
getContentResolver().query android where clause
I also tried

val searchQuery = "${DocumentsContract.Document.COLUMN_DISPLAY_NAME} like '%aaa%'"
val cursor = cResolver.query(uri, arrayOf(DocumentsContract.Document.COLUMN_DISPLAY_NAME), searchQuery, null, null)

But here also I get the same result. It would be great if someone can point me where I am making the mistake.
Thanks.

like image 320
SayantanRC Avatar asked Apr 07 '26 12:04

SayantanRC


1 Answers

A very similar question has been already asked, the unsatisfying answer is that the selection clauses and sort orders are ignored for descendants of the uri (which represents a folder).

like image 131
mathew11 Avatar answered Apr 17 '26 09:04

mathew11