Issue converting URI to Bitmap (2014):

19,178

Solution 1

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

}

Solution 2

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)
Share:
19,178
Admin
Author by

Admin

Updated on August 08, 2022

Comments

  • Admin
    Admin over 1 year

    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.