MediaStore - Uri to query all types of files (media and non-media)
Asked Answered
A

1

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.

Aindrea answered 30/4, 2012 at 12:55 Comment(2)
only non-media files? As in no .jpg .avi .mp3 but only .txt etc ?Quincy
Yeah. Only non-media files. For .jpg, .avi, etc. I can use the Audio, Video and Images classes.Aindrea
Q
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);
Quincy answered 30/4, 2012 at 13:25 Comment(4)
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.Aindrea
By looking at the sourcecodes - Uri.parse("content://media/external/file") or Uri.fromParts("content", "media/external/file", null) should do the sameQuincy
@Kushan2 either drop the where clause and filter in java code or add all the mimetypes to the where clause, e.g. like https://mcmap.net/q/427794/-where-in-clause-in-android-sqlite/995891 - sqlite.org/lang_select.htmlQuincy
The string values are available as constants in MediaStore (e.g. VOLUME_EXTERNAL). See the documentation for more detail.Brantbrantford

© 2022 - 2024 — McMap. All rights reserved.