0

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");
Community
  • 1
  • 1
scientiffic
  • 9,045
  • 18
  • 76
  • 149
  • 1
    Can you see these files in the gallery app? Are you sure they have been added to the mediastore? Simply creating them does not automatically add them to the media store DB. http://stackoverflow.com/questions/3300137/how-can-i-refresh-mediastore-on-android – Ken Wolf Apr 06 '14 at 16:47
  • thanks for the link. actually, I can't see the images in the gallery app. so I'll look into the link and see if it works. – scientiffic Apr 06 '14 at 16:50
  • I think you can also reboot and/or remount your device as a quick way to trigger the mediascanner. – Ken Wolf Apr 06 '14 at 16:52
  • I added this line when I save images in my app, and now they show up in the gallery! sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAddedOrDeleted))); Can you add your comment as an answer? I'll mark it as correct. – scientiffic Apr 06 '14 at 17:01

1 Answers1

1

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?

Community
  • 1
  • 1
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • 1
    May 2021: This no longer works as of targetting Android 11 (R) This constant was deprecated in API level 29. Callers should migrate to inserting items directly into MediaStore, where they will be automatically scanned after each mutation. – Gubatron May 05 '21 at 04:30