JBay Solutions Development Blog on Java, Android, Play2 and others
RSS RSS RSS RSS

Accessing Images in Android External Storage

To access the list of media files (such as Audio, Images and Video) from an External Storage device in android, the Media provider can be used.

An example of using the Media provider to access all Image type files on the External Storage:

Cursor mCursor = getContentResolver()
        .query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null,
            null, 
            null,
            MediaStore.Images.Media.DEFAULT_SORT_ORDER);

Then we can go through the cursos and get all sorts of data from each existing image:

mCursor.moveToFirst();
    while(!mCursor.isAfterLast()) {
        Log.d(TAG, " - _ID : " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media._ID)));
        Log.d(TAG, " - File Name : " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
        Log.d(TAG, " - File Path : " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)));
        mCursor.moveToNext();
    }
    mCursor.close();

In the previous example:

  • MediaStore.Images.Media.DISPLAY_NAME: is the name of the file.
  • MediaStore.Images.Media.DATA: is the full Path of the file.

Each image has an ID associated with it, which can be used to for eample getting the thumbnail of that image: MediaStore.Images.Media._ID


Getting Thumbnail from Media provider

The Media provider also provides access to two types of auto generated Thumbnails for the images:

  • MINI_KIND: 512 x 384
  • MICRO_KIND: 96 x 96

Keep in mind that an image in the Media store might also not have thumbnails.

mCursor = context.getContentResolver()
        .query(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                    null,
                    MediaStore.Images.Thumbnails.IMAGE_ID + "=?" ,
                    new String[]{id}, 
                    null);

Where id is the value of MediaStore.Images.Media._ID of the particular Image in question.

The following code prints a list of the available thumbnails for a specific image:

mCursor.moveToFirst();
while (!mCursor.isAfterLast()){
    Log.d(TAG, "  - Thumbnail File Path : " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
    Log.d(TAG, "  - Thumbnail Type : " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Thumbnails.KIND));
    mCursor.moveToNext();
}
mCursor.close();

Where Thumbnail Kind is an integer:

  • MediaStore.Images.Thumbnails.MICRO_KIND: 3
  • MediaStore.Images.Thumbnails.MINI_KIND : 1


Further reading

Android API reference for Media provider

Media Columns List

Extra Image Specific Columns List



comments powered by Disqus