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 .
Steps
  1. Create New Application and give name it SDImages and Do next. 
    1. minSdkVersion="17” 
    2. targetSdkVersion="17". 
  2. Give activity name – Main.java and main.xml. 
  3. Go to main.xml and paste the below code.

Main.xml



    
    



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 AllImageAdapter.java. always give proper name so you can remember it and when ever you want to change you can can easily change from it.
AllImageAdapter.java

package com.example.samplecollection;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
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;
 }

 @Override
 public int getCount() {
  return cur.getCount();
 }

 @Override
 public Object getItem(int position) {
  return position;
 }

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

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

         imageView.setLayoutParams(new GridView.LayoutParams(330, 330));
         imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
         imageView.setPadding(4, 4, 4, 4);
         //convertView.setTag(imageView);
     return imageView;
 }

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

import android.app.Activity;
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;
 @SuppressWarnings("deprecation")
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.image_gallery);
  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(R.id.GV_Images);
     AllImageAdapter adapter = new AllImageAdapter(Main.this,cursor,columnIndex);  
     AllImages.setAdapter(adapter);
     //adapter.notifyDataSetChanged();
     

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.image_gallery, 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!

    ReplyDelete