14

How do you get a list of all camera images of an Android device?

Is it through the MediaStore? How?

hpique
  • 119,096
  • 131
  • 338
  • 476

1 Answers1

39

The Gallery app obtains camera images by using a content resolver over Images.Media.EXTERNAL_CONTENT_URI and filtering the results by Media.BUCKET_ID. The bucket identifier is determined with the following code:

public static final String CAMERA_IMAGE_BUCKET_NAME =
        Environment.getExternalStorageDirectory().toString()
        + "/DCIM/Camera";
public static final String CAMERA_IMAGE_BUCKET_ID =
        getBucketId(CAMERA_IMAGE_BUCKET_NAME);

/**
 * Matches code in MediaProvider.computeBucketValues. Should be a common
 * function.
 */
public static String getBucketId(String path) {
    return String.valueOf(path.toLowerCase().hashCode());
}

Based on that, here's a snippet to get all camera images:

public static List<String> getCameraImages(Context context) {
    final String[] projection = { MediaStore.Images.Media.DATA };
    final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
    final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
    final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
            projection, 
            selection, 
            selectionArgs, 
            null);
    ArrayList<String> result = new ArrayList<String>(cursor.getCount());
    if (cursor.moveToFirst()) {
        final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        do {
            final String data = cursor.getString(dataColumn);
            result.add(data);
        } while (cursor.moveToNext());
    }
    cursor.close();
    return result;
}

For more info, review the ImageManager and ImageList classes of the Gallery app source code.

hpique
  • 119,096
  • 131
  • 338
  • 476
  • 6
    Not all devices save the taken images/videos to "/DCIM/Camera". HTC desire HD, for example, saves to "/DCIM/100MEDIA" – AlikElzin-kilaka Jul 10 '11 at 13:42
  • 32
    And we should hate HTC Desire HD for that. – hpique Jul 10 '11 at 19:05
  • 3
    You can, however, just list all the files using File api instead of using DB cursors. – AlikElzin-kilaka Jul 11 '11 at 12:31
  • Can you please explain how to get the original image path from thumb? On this line: final String data = cursor.getString(dataColumn); you get the path to the thumbnail image. I need, in my app, to display the thumbnail image (faster) and to save a copy of the image used to generate that thumbnail. – Zelter Ady Apr 23 '12 at 21:38
  • @hpique It's not just HTC devices that have different CAMERA_IMAGE_BUCKET_NAME strings. My Jelly Bean Galaxy Nexus returns just "Camera" for example. – Matthew May 07 '13 at 06:18
  • links mentioned are broken – Dakait Apr 24 '14 at 06:21
  • @dakait Links removed. I don't know where the Gallery source code is now. Any ideas? – hpique Apr 24 '14 at 08:51
  • nope did a quick search could find them :) if you are referring to stock gallery app them i think it must be somewhere https://github.com/android – Dakait Apr 24 '14 at 09:53
  • @dakait According to github search they're not there. – hpique Apr 24 '14 at 09:55