Mediastore Content Provider to retrieve Images from SDCard and Displaying in Gridview

Following code demonstrates retrieving image files from sdcard and displaying in listview. When user selects any of the listview item that image file should be played as Full View in Next Activity.

To run this program pull some image files to emulator using DDMS perspective.
Goto DDMS perspective Choose Devices tab -> choose emulator on File Explorer tab
choose /mnt/sdcard and pull image file by clicking on Pull a file to device.

Layout file Activity_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"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

MainActivity.java

package com.example.mediastoreimage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
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;

public class MainActivity extends Activity {

	Cursor c;
	int count;
	int columnIndex;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		GridView gv = (GridView)findViewById(R.id.gridView1);
		String cols[] = {MediaStore.Images.Thumbnails.DATA};
		c = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cols,null,null,null);
		count = c.getCount();
		gv.setAdapter(new ImageAdapter(getApplicationContext()));
		gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				c.moveToPosition(arg2);
				String s = c.getString(0);
				Intent i = new Intent(getApplicationContext(),DisplayImage.class);
				i.putExtra("img", s);
				startActivity(i);
				
			}
		});
		
		
	}
	private class ImageAdapter extends BaseAdapter
	{
		Context image_context;
		private ImageAdapter(Context c)
		{
			image_context = c;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return count;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView iv = new ImageView(image_context.getApplicationContext());
			c.moveToPosition(position);
			String s = c.getString(0);//c.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
			Bitmap b = BitmapFactory.decodeFile(s);
			iv.setImageBitmap(b);
			iv.setPadding(2, 2, 2, 2);
			iv.setLayoutParams(new GridView.LayoutParams(100,100));
			return iv;
		}
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

DisplayImage.java


package com.example.mediastoreimage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class DisplayImage extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    String s = getIntent().getExtras().getString("img");
	    ImageView iv = new ImageView(getApplicationContext());
	    Bitmap bitmap = BitmapFactory.decodeFile(s);
	    iv.setImageBitmap(bitmap);
	    setContentView(iv);
	    
	}

}
Advertisements