How to access the Media Galery in Android

In this tutorial I’ll show how we can access the media galery, pick a picture, and display it in our activity.

The final result will look like this:

1. Create a new project in Eclipse
Project name: MediaGalery 
Activity: MediaGaleryActivity.

2. Add a Button and an ImageView control to the main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/openGalery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Media Galery" />

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"
/>

</LinearLayout>

3. Open the MediaGaleryActivity and make it implement the OnClickListener

public class MediaGaleryActivity extends Activity implements OnClickListener {

4. Just after the class declaration, add the following instance variables:

private static final int SELECT_IMAGE = 1;
Button openGalery;

5. Add following lines of code in the onCreate() method:

openGalery = (Button) findViewById(R.id.openGalery);
openGalery.setOnClickListener(this);

6. Below the onCreate() method, add the onClick() method:

//When the Open Media Gallery button is cliked, open the media galery

@Override
public void onClick(View v) {
  switch (v.getId()) {
  case R.id.openGalery:
  Intent gallery = new Intent(
  Intent.ACTION_PICK,
  android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
  startActivityForResult(gallery, SELECT_IMAGE);
  break;

  default:
    break;
}
}

7. Below the onClick() method, add following 2 methods.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  if(resultCode==RESULT_OK && requestCode==SELECT_IMAGE){
   Uri selectedImage=data.getData();
   String path=getPath(selectedImage);

   Bitmap bitmapImage=BitmapFactory.decodeFile(path);
   ImageView image=(ImageView)findViewById(R.id.image);
   image.setImageBitmap(bitmapImage);

  }
}

public String getPath(Uri uri){
  String[] filePathColumn={MediaStore.Images.Media.DATA};

  Cursor cursor=getContentResolver().query(uri, filePathColumn, null, null, null);
  cursor.moveToFirst();
  int columnIndex=cursor.getColumnIndex(filePathColumn[0]);

  return cursor.getString(columnIndex);
}


4 thoughts on “How to access the Media Galery in Android

  1. You really make it appear really easy with your presentation but I in finding this matter to be actually something that I think I would never understand. It seems too complex and extremely broad for me. I am having a look forward to your next put up, I will attempt to get the hold of it!

    1. Hi Annabelle,
      Thank you for your comment.

      The tutorial assumes the user have basic knowledge about how to set up and Android project and how to work with the main components of a project, like layouts and activities.

      If you don’t have any prior experience with Android, I recommend you experiment with some “Hello World” tutorials, and then come back.

      Thank you for your comment again, maybe I should think about writing a step-by-step “Hello World” android tutorial.

Leave a comment