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.
To bind your button click listener: (This should be in your onCreate method.)
ImageButton btn_choose_photo = (ImageButton) findViewById(R.id.add_photo_choose_photo); // Replace with id of your button.
btn_choose_photo.setOnClickListener(btnChoosePhotoPressed);
To open gallery: (This should be in your activity class.)
public OnClickListener btnChoosePhotoPressed = new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
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);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
/* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */
}
}
};