fetching images from gallery on android phones with internal storage

sanjana picture sanjana · Aug 14, 2013 · Viewed 12.6k times · Source

Hi I am developing an Android Gallery app where I am fetching images from built in gallery and displaying it.I am using the code as below

        String[] projection = {MediaStore.Images.Thumbnails._ID};


   Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,       
                null);


        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        int size = cursor.getCount();
        // If size is 0, there are no images on the SD Card.
        if (size == 0) 
        {
            Log.e("size 0","0");
        }

The Problem is when I run this code on Phones having only internal storage (Galaxy Nexus) I am getting Log which says size as Zero even though there are images in built in gallery. How do I resolve this. Please Help.Thanks!

Answer

Arun C picture Arun C · Aug 14, 2013

To get list of Gallery images you can try this

String[] projection = new String[]{
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.Images.Media.DATE_TAKEN
};


Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;


Cursor cur = getContentResolver().query(imageUri,
        projection, // Which columns to return
        null,       // Which rows to return (all rows)
        null,       // Selection arguments (none)
        null        // Ordering
        );

Log.i("Images Count"," count="+cur.getCount());