Monday, June 4, 2012

how to pick a image from gallery (SD Card) for my app in android?

This question was originally asked for Android 1.6

i am working on photos options in my app.

i have a button and imageview on my activity. when i click the button it will redirect to gallery and i would like to select an image. the selected image will appear in my image view.

how to do that? any idea?

Source: Tips4all


  1. The other answers explained how to send the intent, but they didn't explain well how to handle the response. Here's some sample code on how to do that:

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

    switch(requestCode) {
    if(resultCode == RESULT_OK){
    Uri selectedImage = imageReturnedIntent.getData();
    String[] filePathColumn = {MediaStore.Images.Media.DATA};

    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

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

    Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);

    After this, you've got the selected image stored in "yourSelectedImage" to do whatever you want with. This code works by getting the location of the image in the ContentResolver database, but that on its own isn't enough. Each image has about 18 columns of information, ranging from its filepath to 'date last modified' to the GPS coordinates of where the photo was taken, though many of the fields aren't actually used.

    To save time as you don't actually need the other fields, cursor search is done with a filter. The filter works by specifying the name of the column you want, MediaStore.Images.Media.DATA, which is the path, and then giving that string[] to the cursor query. The cursor query returns with the path, but you don't know which column it's in until you use the columnIndex code. That simply gets the number of the column based on its name, the same one used in the filtering process. Once you've got that, you're finally able to decode the image into a bitmap with the last line of code I gave.

  2. You have to start the gallery intent for a result.

    Intent i = new Intent(Intent.ACTION_PICK,
    startActivityForResult(i, ACTIVITY_SELECT_IMAGE);

    Then in onActivityForResult, call intent.getData() to get the Uri of the Image. Then you need to get the Image from the ContentProvider.

  3. private static final int SELECT_PHOTO = 100;

    Start intent

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    startActivityForResult(photoPickerIntent, SELECT_PHOTO);

    Process result

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

    switch(requestCode) {
    case SELECT_PHOTO:
    if(resultCode == RESULT_OK){
    Uri selectedImage = imageReturnedIntent.getData();
    InputStream imageStream = getContentResolver().openInputStream(selectedImage);
    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);

    Alternatively, you can also downsample your image to avoid OutOfMemory errors.

    private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 140;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
    if (width_tmp / 2 < REQUIRED_SIZE
    || height_tmp / 2 < REQUIRED_SIZE) {
    width_tmp /= 2;
    height_tmp /= 2;
    scale *= 2;

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);


  4. Do this to launch the gallery and allow the user to pick an image:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, IMAGE_PICK);

    Then in your onActivityResult() use the URI of the image that is returned to set the image on your ImageView.

  5. public class EMView extends Activity {
    ImageView img,img1;
    int column_index;
    Intent intent=null;
    // Declare our Views, so we can access them later
    String logo,imagePath,Logo;
    Cursor cursor;
    private static final int SELECT_PICTURE = 1;

    String selectedImagePath;
    String filemanagerstring;

    public void onCreate(Bundle savedInstanceState) {
    img= (ImageView)findViewById(;

    ((Button) findViewById(
    .setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

    // in onCreate or any event where your want the user to
    // select a file
    Intent intent = new Intent();
    "Select Picture"), SELECT_PICTURE);


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
    if (requestCode == SELECT_PICTURE) {
    Uri selectedImageUri = data.getData();

    //OI FILE Manager
    filemanagerstring = selectedImageUri.getPath();

    selectedImagePath = getPath(selectedImageUri);


    TextView txt = (TextView)findViewById(;

    Bitmap bm = BitmapFactory.decodeFile(imagePath);

    // img1.setImageBitmap(bm);




    public String getPath(Uri uri) {
    String[] projection = { MediaColumns.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    column_index = cursor
    imagePath = cursor.getString(column_index);

    return cursor.getString(column_index);


  6. look at this answer, i posted an improved code there to handle picks from file managers also

    Open an image in Android's built-in Gallery app programmatically