Saturday 22 September 2012

Android-Fetch images from sdcard and display in gridview


Using this code you can fetch images from sdcard and display it in a gridview
Sdcard.java
package com.example.sdcard;



import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class Sdcard extends Activity {

    //  Cursor used to access the results from querying for images on the SD card.

    private Cursor cursor;
   
     // Column index for the Thumbnails Image IDs.
 
    private int columnIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdcard);

        // Set up an array of the Thumbnail Image ID column we want
        String[] projection = {MediaStore.Images.Thumbnails._ID};
        // Create the cursor pointing to the SDCard
        cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,
                MediaStore.Images.Thumbnails.IMAGE_ID);
        // Get the column index of the Thumbnails Image ID
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

        GridView sdcardImages = (GridView) findViewById(R.id.gridView1);
        sdcardImages.setAdapter(new ImageAdapter(this));


    }




// Image adapter to link images to the gridview 


    private class ImageAdapter extends BaseAdapter {

        private Context context;

        public ImageAdapter(Context localContext) {
            context = localContext;
        }

        public int getCount() {
            return cursor.getCount();
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView picturesView;
            if (convertView == null) {
                picturesView = new ImageView(context);
                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // Set the content of the image based on the provided URI
                picturesView.setImageURI(Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
                picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
                picturesView.setPadding(10, 10, 10, 10);
                picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
            }
            else {
                picturesView = (ImageView)convertView;
            }
            return picturesView;
        }
    }
}


Here is the main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
   
        android:columnWidth="90dp"
         android:stretchMode="columnWidth"
         android:gravity="center"
        android:numColumns="2" >
    </GridView>

</RelativeLayout>


Screenshot for the Application

29 comments:

  1. for Imageadapter.java class,we have to create another java class or else can we write in the existing class?

    ReplyDelete
  2. The Images are repeating twice on my xperia ray (as an emulator)
    I mean, the onCreate method is called twice. and also, it does not load all images on my sdcard, any idea how? and also, how about in terms of videos? thanks!

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. images r repeated,not a working code sorry

    ReplyDelete
  5. Finally a working solution, thanks!

    ReplyDelete
  6. The error says its Permission error But I have Listed the permission in manifest file.

    ReplyDelete
    Replies
    1. did you take permission both of them permission....

      write-external-storage and read-external-storage

      Delete
  7. Why are images repeating twice? Please Reply...

    ReplyDelete
  8. Any idea how to delete the selected gridview item onLongClick from the SD card permanently?

    ReplyDelete
  9. Any idea how to delete the selected gridview item onLongClick from the SD card permanently?

    ReplyDelete
  10. if I want to retrieve images from a specific folder from SD card?

    ReplyDelete
  11. error was in
    cursor.moveToPosition(position);
    int imageID = cursor.getInt(columnIndex);

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Followed the same code and i got this error..plz help me

    Unable to open content: content://media/external/images/thumbnails/2
    java.io.FileNotFoundException: No such file or directory
    in the getView method of ImageAdapter class

    ReplyDelete
  14. if i want to get images from specific folder then where to change?

    ReplyDelete
  15. Thank u...how do i retrieve image in a particular folder from sd card

    ReplyDelete
  16. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
    Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

    ReplyDelete
  17. Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of....
    Data science training chennai | data science course chennai

    ReplyDelete
  18. very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.Java Training in Chennai

    Java Training in Velachery

    Java Training inTambaram

    Java Training in Porur

    Java Training in Omr

    Java Training in Annanagar


    ReplyDelete
  19. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Software Testing Training in Chennai

    Software Testing Training in Velachery

    Software Testing Training in Tambaram

    Software Testing Training in Porur

    Software Testing Training in Omr

    Software Testing Training in Annanagar

    ReplyDelete