Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue converting URI to Bitmap (2014):

In short, I'm trying to select an image from a phone gallery to display as a bitmap to be played with (get average RGB) in another activity.

First thing, I've come across a couple topics dealing with URI to Bitmap conversion. A lot of them have suggestions like (from: Retrieve bitmap from uri):

    Uri imageUri = intent.getData();
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
    Imageview my_img_view = (Imageview ) findViewById (R.id.my_img_view);
    my_img_view.setImageBitmap(bitmap);

The Bitmap line is the important line. Whenever I run my Android app on my simulator, the app crashes (debugger in Eclipse confirms in happens on the URI --> Bitmap conversion line), and if I put the conversion in different activities (it is Bundled), then it still crashes on the Uri -> Bitmap conversion line.

I'm not sure why this is. I've tried making my intent both "EXTERNAL_CONTENT_URI" and "INTERNAL_CONTENT_URI" on the initial intent, and either choice doesn't matter. I'm going to keep looking for potential solutions to my problem, but I'm short on time right now and I feel it would be helpful if I had some advice from outside sources.

Anyone know why it always crashes on that line, or if there are any possible solutions to my problem? Thanks.

like image 605
Nodetails Avatar asked Sep 13 '14 23:09

Nodetails


2 Answers

Please get inputstream from the uri

Uri IMAGE_URI = imageReturnedIntent.getData();
InputStream image_stream = getContentResolver().openInputStream(IMAGE_URI);
Bitmap bitmap= BitmapFactory.decodeStream(image_stream );

Start intent

private static final int REGUEST_CODE = 100;
Intent photoPicker = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REGUEST_CODE); 

GET THE RESULT

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
switch(requestCode) { 
case REGUEST_CODE :
    if(resultCode == RESULT_OK){  
        Uri IMAGE_URI = imageReturnedIntent.getData();
        InputStream image_stream = getContentResolver().openInputStream(IMAGE_URI);
        Bitmap bitmap= BitmapFactory.decodeStream(image_stream );
        my_img_view.setImageBitmap(bitmap)
    }
}

}

like image 68
sujith s Avatar answered Nov 01 '22 02:11

sujith s


Uri imageUri = intent.getData(); Uri IMAGE_URI = imageReturnedIntent.getData();

InputStream image_stream = getContentResolver().openInputStream(IMAGE_URI);

Bitmap bitmap= BitmapFactory.decodeStream(image_stream );



Intent photoPicker = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REGUEST_CODE); 

GET THE RESULT

Uri IMAGE_URI = imageReturnedIntent.getData();
InputStream image_stream = getContentResolver().openInputStream(IMAGE_URI);
Bitmap bitmap= BitmapFactory.decodeStream(image_stream);
my_img_view.setImageBitmap(bitmap)
like image 39
Jinu Avatar answered Nov 01 '22 01:11

Jinu