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=""
android:orientation="vertical" >

android:text="Open Media Galery" />



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(;

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

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

public void onClick(View v) {
  switch (v.getId()) {
  Intent gallery = new Intent(
  startActivityForResult(gallery, SELECT_IMAGE);


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

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(;


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

  Cursor cursor=getContentResolver().query(uri, filePathColumn, null, null, null);
  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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s