Error converting URI to Bitmap (2014):

In short, I am trying to select an image from the phone’s gallery to display it as a bitmap that needs to be reproduced (get the average RGB value) in another activity.

Firstly, I came across several topics regarding the URI for Bitmap conversion. Many of them have suggestions such as (from: Extract 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 an important line. Whenever I run my Android application on my simulator, the application crashes (the debugger in Eclipse confirms what happens in the URI → Bitmap conversion string), and if I put the conversion in different actions (it is included), then it’s all equally will fail in the conversion string Uri → Bitmap.

I am not sure why this is so. I tried to make my intentions both "EXTERNAL_CONTENT_URI" and "INTERNAL_CONTENT_URI" in the original intention, and any choice does not matter. I will continue to search for potential solutions to my problem, but now I have a short time, and I feel that it would be useful if I had any advice from external sources.

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

+7
android image bitmap
source share
3 answers

Please enter login with 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 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) } } 

}

+7
source share
 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 RESULT

 Uri IMAGE_URI = imageReturnedIntent.getData(); InputStream image_stream = getContentResolver().openInputStream(IMAGE_URI); Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(IMAGE_URI)); my_img_view.setImageBitmap(bitmap) 
+3
source share

How big is the bitmap you're trying to get through? Can you copy / paste the error message you receive?

One possible reason your application crashes is that the data that you can transfer in main memory from one activity to another is limited in size. I believe that the maximum data is about 1-2 MB, depending on the hardware and OS version, although I can not find the link.

0
source share

All Articles