Android – Pick image from native gallery

By -



Problem: How i pick image from native gallery and display it inside the ImageView?
Description:

First of all, to launch the native applications for Gallery, just write the below, there may be having many applications if you have installed other gallery like application, for example: QuickPic or any gallery application.

Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PICTURE);

Now we have to fetch the selected image inside the onActivityResult() method by overriding it inside our activity class.

Output:
Android - Select image from Gallery

Android - Select image from Gallery

Android - Select image from Gallery

GalleryImagePickActivity.java

package com.paresh.galleryimagepick;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

public class GalleryImagePickActivity extends Activity {
    /** Called when the activity is first created. */

	private static final int SELECT_PICTURE = 1;
	 ImageView imgBG;

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

        imgBG = (ImageView) findViewById(R.id.imgBG);
    }

    public void btnChooseBGClick(View v)
    {
    	Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECT_PICTURE);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch(requestCode) {
        case SELECT_PICTURE:
            if(resultCode == RESULT_OK)
            {
                Uri uri = imageReturnedIntent.getData();
                String[] projection = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(projection[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                Drawable d =new BitmapDrawable(yourSelectedImage);
                imgBG.setBackgroundDrawable(d);
            }
        }
    }
}

main.xml




    

    
    


Download Full source code from here: Android – Pick image from native gallery



Paresh Mayani

Lazy android developer, exploring the horizon of android development since 5 years. He is Senior Software engineer at InfoStretch Solutions Pvt. Ltd. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...