Is it possible to call the default gallery from the code, so that the user will be shown all the pictures of his gallery to select one picture. As a result I need to receive the path of the selected picture so that i can do my processing with it.
Thanks
Use below code to create and fire a intent to start Gallery
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 0);
and below line in onActivityResult(int requestCode, int resultCode, Intent data)
method to fetch URI
Uri dataUri = data.getData(); //Image URI
You would most probably need below method to convert this Image URI to small size bitmap . if showing image in your display, as without it , it could result in OOM exception
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) {
break;
}
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);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments