How to Fetch Images From Storage ?

There are number of Gallery Application In Android Market. now you can also make it your own. To fetch and display All images from internal or external storage you have to create cursor that fetch images from your storage and then give you index. from this tutorial you can get how to fetch images from storage and display in List view or Grid view. so go throw full tutorial and learn how you can create your gallery application .
  1. Create New Application and give name it SDImages and Do next. 
    1. minSdkVersion="17” 
    2. targetSdkVersion="17". 
  2. Give activity name – and main.xml. 
  3. Go to main.xml and paste the below code.



Now your layout is set. it will display two columns. but you can change it from above layout . in grid view you can see numColumns attribute change it to what ever number you want to set. make sure user can see images clear. now to display images we have to create Adapter class and that will attach images to each grid of Grid view. so create new class and give name to it always give proper name so you can remember it and when ever you want to change you can can easily change from it.

package com.example.samplecollection;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class AllImageAdapter extends BaseAdapter {

 Context c;
 Cursor cur;
 ImageView imageView;
 int columnIndex;
 public AllImageAdapter(Context c, Cursor cur, int columnIndex)
  this.c = c;
  this.cur = cur;
  this.columnIndex = columnIndex;

 public int getCount() {
  return cur.getCount();

 public Object getItem(int position) {
  return position;

 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return 0;

 public View getView(int position, View convertView, ViewGroup parent) {
  // Set The Image in Image View.
         imageView = new ImageView(c);
         int imageID = cur.getInt(columnIndex);
         imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

         imageView.setLayoutParams(new GridView.LayoutParams(330, 330));
         imageView.setPadding(4, 4, 4, 4);
     return imageView;

Now you have to copy below code in your file and check if you want change some thing or not.
package com.example.samplecollection;

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

public class Main extends Activity {

 private Cursor cursor;
 private  int columnIndex;
 GridView AllImages;
 protected void onCreate(Bundle savedInstanceState) {
  String[] projection = {MediaStore.Images.Thumbnails._ID};
     cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails._ID + "");
     columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
     AllImages = (GridView) findViewById(;
     AllImageAdapter adapter = new AllImageAdapter(Main.this,cursor,columnIndex);  


 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(, menu);
  return true;


So that's it. it is really easy to fetch all images from SD card and then display it in your application. in below screenshot i run this app in emulator and it having only two images so it display only to but run this app in your mobile device and then check this out. if you like this tutorial so stay connected with us or subscribe to my blog for future tutorials.
Keep Coding...
Screen Shot

1 comment :

  1. @SuppressWarnings("deprecation") lovely!