Search code examples
javaandroidmediastoreandroid-11

Querying Mediastore for a file created outside the App returns nothing


Querying MediaStore.Downloads for a file uploaded throught the Device Explorer (and thus created outside the App) returns nothing.

If the file is rather created via the .insert method by the App the same query returns the file as expected.

Is this some kind of permission issue there?

File location is = \Download\MyApp\MyFile.csv

        String selection = MediaStore.Downloads.RELATIVE_PATH + " LIKE ? ";
        // appfoldername = "MyApp"
        String[] selectionArgs = new String[]{
                '%' + Environment.DIRECTORY_DOWNLOADS + File.separator + appfoldername + '%'
                 };

        Cursor cursor  = resolver.query(MediaStore.Downloads.EXTERNAL_CONTENT_URI,
                new String[] {MediaStore.MediaColumns.RELATIVE_PATH, MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.MIME_TYPE}, selection, selectionArgs, null);

Solution

  • Quoting the documentation:

    If your app wants to access a file within the MediaStore.Downloads collection that your app didn't create, you must use the Storage Access Framework. To learn more about how to use this framework, see the guide on how to access documents and other files.