Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 613
Shyam Prasad Murarka Avatar asked Apr 30 '12 12:04

Shyam Prasad Murarka


1 Answers

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); 
like image 144
zapl Avatar answered Sep 22 '22 15:09

zapl