36

In the class MediaStore.Files class, its mentioned that,

Media provider table containing an index of all files in the media storage, including non-media files.

I'm interested in querying for non-media files like PDF.

I'm using CursorLoader to query the database. The second parameter for the constructor requires an Uri argument which is easy to get for the media types Audio, Images and Video as each of them have a EXTERNAL_CONTENT_URI and INTERNAL_CONTENT_URI constant defined for them.

For MediaStore.Files there is no such defined constant. I tried using the getContentUri() method but couldn't figure out the argument value for volumeName. I tried giving "/mnt/sdcard" and also the volume name that appears when I connect the device to my system but in vain.

I saw a similar question on Google Groups but that is not resolved.

EDIT: I also tried using Uri.fromFile(new File("/mnt/sdcard/")) and Uri.parse(new File("/mnt/sdcard").toString()) but that didn't work out either.

1 Answers1

60

It is "external" or "internal" although internal (system files) is probably not useful here.

ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");

// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
String[] projection = null;

// exclude media files, they would be here also.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
        + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null; // there is no ? in selection so null here

String sortOrder = null; // unordered
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);

If you want .pdf only you could check the mimetype

// only pdf
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{ mimeType };
Cursor allPdfFiles = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
zapl
  • 63,179
  • 10
  • 123
  • 154
  • Thanks a lot! :) How did you figure that out? I searched quite a lot on the Internet. I did come across "external" but used it like this Uri.fromParts("content", "external", "") which didn't work out. – Shyam Prasad Murarka Apr 30 '12 at 13:42
  • 1
    By looking at the sourcecodes - `Uri.parse("content://media/external/file")` or `Uri.fromParts("content", "media/external/file", null)` should do the same – zapl Apr 30 '12 at 13:51
  • @Kushan2 either drop the where clause and filter in java code or add all the mimetypes to the where clause, e.g. like http://stackoverflow.com/q/6258856/995891 - https://sqlite.org/lang_select.html – zapl Apr 17 '17 at 10:19
  • The string values are available as constants in `MediaStore` (e.g. `VOLUME_EXTERNAL`). See the [documentation](https://developer.android.com/reference/kotlin/android/provider/MediaStore#volume_external) for more detail. – jkemming Dec 28 '20 at 19:54