0 0 0 0


I wanna to open phones gallery through a button click.
In my activity I have a button, I want to open the gallery through that button click.

Best Answer:

To bind your button click listener: (This should be in your onCreate method.)

ImageButton btn_choose_photo = (ImageButton) findViewById(; // Replace with id of your button.

To open gallery: (This should be in your activity class.)

public OnClickListener btnChoosePhotoPressed = new OnClickListener() {      
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_PICK,
        final int ACTIVITY_SELECT_IMAGE = 1234;
        startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 

To get the chosen image: (This should be in your activity class.)

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode) { 
    case 1234:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = data.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);
            /* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */

Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs