Question

I'm using the following example to try to load saved images and video on an android device into a customized gallery activity:

Access ordered images and video in same Cursor

While this works for images and video created using the default Android camera app, it doesn't seem to find media files that are saved locally in a folder specifically for the app (located in Pictures/Name_of_app)

How can I fetch all media files that are on the device?

private String[] projection = {
            MediaStore.Files.FileColumns._ID, 
            MediaStore.Files.FileColumns.DATA,
            MediaStore.Files.FileColumns.DATE_ADDED,
            MediaStore.Files.FileColumns.MEDIA_TYPE,
            MediaStore.Files.FileColumns.MIME_TYPE,
            MediaStore.Files.FileColumns.TITLE
    }; // used for query ContentResolver for mediafiles

    private String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
            + " OR " 
            + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;  // used to select images and videos from contentResolver

    private Uri queryUri = MediaStore.Files.getContentUri("external");  

 cl = new CursorLoader(CustomizedGallery.this, queryUri, projection, selection, null, MediaStore.Files.FileColumns.DATE_ADDED + " DESC");
Was it helpful?

Solution

It sounds like the files haven't been added to the MediaStore database yet. You can check this by checking the gallery app - if they don't show up there they aren't in the DB yet.

Simply creating them does not automatically add them to the database - scans happen at regular trigger points(such as on reboot or remount of the external storage) or you can manually request a rescan.

To request a scan of a particular file you can call:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(newFile)));

More information here: How can I refresh MediaStore on Android?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top