Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instant upload and Google+ images in my app

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?

like image 838
Jeroen Avatar asked Nov 29 '12 11:11

Jeroen


1 Answers

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).

like image 60
f2prateek Avatar answered Sep 28 '22 06:09

f2prateek