Feeds:
Posts
Comments

Archive for the ‘query media store content provider for images’ Category

During one of the training programs someone asked me how to query the MediaStore database for images. So, i thought of writing a small program for the same.

The database or Content URI for the Images is MediaStore.Images.Media.EXTERNAL_CONTENT_URI .
This is the content:// style URI for the “primary” external storage volume.
The querying method is same like querying any other native content provider.
First obtain the ContentResolver object by using getContentResolver();
We can then query the contentresolver object as follows:


Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				
                         null, null, null, null);

The Images DataStore has a couple of columns. Some of the main columns are:
1.MediaStore.Images.ImageColumns._ID– This represent the Unique ID of the row.
2.MediaStore.Images.Media.DATE_TAKE– The date when the image was captured. The same is in long miliseconds.
3.MediaStore.Images.Media.LATITUDE– The latitude where the image was captured.
4.MediaStore.Images.Media.LONGITUDE– The longitude where the image was captured.
5.MediaStore.Images.Media.DESCRIPTION-The description of the image
For example, if we want to get the date of the image taken, then we would iterate the cursor and query the date column as follows:


String date = cursor.getString(cursor
						.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));

A major point to remember here is that when we are working with bitmaps , if we do not recycle them then we might see OutOfMemory issues such as OutOfMemoryError: bitmap size exceeds VM budget.
To avoid memory leaks remember to unbind the drawables and recycle the bitmaps in Activity onDestroy method. (REF:Avoid MemoryLeak)

I have posted the source code of the QueryMedia at :

Download source code

Browse Source code:

The same queries the SDcard for all the images stored in it and display them in the gridview along with the date they were captured.
If you are running this code on emulator please ensure that you have some images in your emulator sdcard.

Advertisements

Read Full Post »