0

I'm trying to get a specific file by its name using the following code, however, it looks like the selection arguments are ignored as it always returns the 1st file of the root tree

final ContentResolver resolver = mContext.getContentResolver();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(mUri, DocumentsContract.getDocumentId(mUri));

Cursor c = null;
try {
    c = resolver.query(childrenUri, new String[] { DocumentsContract.Document.COLUMN_DOCUMENT_ID }, DocumentsContract.Document.COLUMN_DISPLAY_NAME + " = ?", new String[] { displayName }, null);
    if (c.moveToFirst()) {
        final String documentId = c.getString(0);
        final DocumentFile result = new TreeDocumentFile(this, mContext, DocumentsContract.buildDocumentUriUsingTree(mUri, documentId));
        return result;
    }
} catch (Exception e) {
    Log.w(TAG, "Failed query: " + e);
} finally {
    closeQuietly(c);
}

Basically the result is the same that the following query

c = resolver.query(childrenUri, new String[] { DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null. null, null);

Any idea why?

user1026605
  • 1,633
  • 4
  • 22
  • 58
  • `ContentResolver ignoring selection argument ..` Dont blame the content resolver. Blame the content provider. The content resolver is only an intermediate between your app and the content provider. Look at authority of used uri to know the provider. – blackapps Nov 04 '21 at 14:56
  • and how to know which ContentProvider is being used? I checked and it's not the FileProvider. my uri is content://com.android.externalstorage.documents/tree/xxxx%3Asubfolder/document/xxxx%3Asubfolder/children – user1026605 Nov 04 '21 at 15:28
  • The authority in this uri is `com.android.externalstorage.documents`. So that is the provider. (Well for me..). – blackapps Nov 04 '21 at 15:36
  • Where can I see the code of this provider then? – user1026605 Nov 04 '21 at 17:24
  • In principle you cant but as this provider belongs to Android source it should be on the internet. – blackapps Nov 04 '21 at 19:55
  • Fiddled around with your code today and indeed providing selection arguments does nothing. Like with all null. Strange. Found something already? – blackapps Nov 05 '21 at 15:16

0 Answers0