I am currently running into the following problem: When I want to retrieve an image from the gallery I use the following code to start the intent for the gallery.
public void useGallery() {
this.intentbasedleave=true;
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, getString(R.string.pleaseselect_image)), IMAGE_PICK);
}
When I get the data from the gallery I use this method:
private void imageFromGallery(int resultCode, Intent data) {
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();
this.updateImageView(BitmapFactory.decodeFile(filePath));
}
This works, unless the image chosen comes from Google+ or instant uploads. Then the BitmapFactory.decodeFile(filePath)) seems null? as the method raises a nullpointer exception.
My question therefore is: how can I use images from Google+ and from instantuploads from the gallery?
Use BitmapFactory.decodeUri
instead of BitmapFactory.decodeFile
.
You can simplify your method imageFromGallery
to
private void imageFromGallery(int resultCode, Intent data) {
Uri selectedImage = data.getData();
updateImageView(BitmapFactory.decodeUri(getContext().getContentResolver(), selectedImage));
}
(Assuming you have access to context from somewhere).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With