Asynchronously loading Image thumbnails in a GridView from an external (SD Card) storage using Media Content Providers in Android

August 20, 2012

In this article, we will see how to display the thumbnails of images stored in external ( SD Card ) storage. The thumbnails are loaded to the gridview using loaders and media content providers. The loaders are used to asynchronously load gridview with images.

An extension to this application is discussed in the article “Loading thumbnail Images in a GridView and Opening original images in AlertDialog using Media Content Providers

The application discussed in this article is developed using Eclipse  IDE ( 4.2.0 ) with ADT plugin ( 20.0.3 ) and Android SDK ( R20.0.3 ) .

1. Create new Android application project titled “GridViewLoadImages”

Create new Android application project

Figure 1 : Create new Android application project

2. Design application launcher icon

Design Application Launcher Icon

Figure 2 : Design Application Launcher Icon

3. Create a blank activity

Create a blank Activity

Figure 3 : Create a blank Activity

4. Enter MainActivity details

Enter MainActivity Details

Figure 4 : Enter MainActivity Details

5. Update the file res/values/strings.xml

    <string name="app_name">GridViewLoadImages</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">Loading GridView with Images</string>
    <string name="img_description">Image Description</string>

6. Update the MainActivity layout file res/layout/activity_main.xml

<GridView xmlns:android=""

7. Create and update the layout file res/layout/gridview.xml with the given below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

8. Update the class MainActivity in the file src/in/wptrafficanalyzer/gridviewloadimages/

package in.wptrafficanalyzer.gridviewloadimages;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.widget.GridView;

public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor>{

    SimpleCursorAdapter mAdapter;

    public void onCreate(Bundle savedInstanceState) {

        /** Getting a reference to gridview of the MainActivity layout */
        GridView gridView = (GridView) findViewById(;

        /** Create an adapter for the gridview */
        /** This adapter defines the data and the layout for the grid view */
        mAdapter = new SimpleCursorAdapter(
            new String[] { "_data"} ,
            new int[] {},

        /** Setting adapter for the gridview */

        /** Loader to get images from the SD Card */
        getSupportLoaderManager().initLoader(0, null, this);


    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

    /** A callback method invoked by the loader when initLoader() is called */
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
        return new CursorLoader(this, uri, null, null, null, null);

    /** A callback method, invoked after the requested content provider returned all the data */
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {

    public void onLoaderReset(Loader<Cursor> arg0) {
        // TODO Auto-generated method stub


9. Screenshot of the application

Asynchronously loaded images from SD Card

Figure 5 : Asynchronously loaded images from SD Card

10. Download

11. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , , , , , ,

6 Responses to Asynchronously loading Image thumbnails in a GridView from an external (SD Card) storage using Media Content Providers in Android

  1. MO on August 26, 2012 at 2:05 am

    i did exactly what you did. but it dosent load the images

  2. anand rai on December 15, 2012 at 11:04 am

    great! Its totally Working but images in grid view are different size.

    • S.Kale on July 18, 2014 at 6:52 pm

      try scaling the image before setting the image in the gridview.
      imageView.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), reqWidth, reqHeight, false));

  3. Jinal Jogiyani on June 24, 2013 at 3:29 pm

    I got class not found run time error…!! I downloaded full code of app.

  4. zerouno on October 29, 2013 at 12:16 pm

    how to use this for load image from my server?


  5. Sankar on March 19, 2015 at 1:58 pm

    How to set specific path …

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner