Thursday 15 January 2015

Android error: java.lang.OutOfMemoryError: bitmap size exceeds VM budget -


I have come across many questions about this error in StackVareflow, but none of them understood the proper solution for my scenario Did not get.

In my Android application, I have the user permission to click on a button and select an image to open the gallery. And then load that particular selected image into an image view in my layout (UI).

It's okay to do this using the following code to get it.

Click the upload button - & gt;

  intent = new intent (); Intent.setType ("image / *"); Intent.setAction (Intent.ACTION_GET_CONTENT); StartActivityForResult (Intent.createChooser (meaning, "Choose Image"), REQUEST_UPLOAD_IMG);   

and then the activity special - & gt;

  Protected over @OverrideActivityResult; zero (int requestcode, integer result code, intent data) {//super.onActivityResult(requestCode, resultcode, data); If (resultCode == activity. RESULT_OK) {if (requestcode == REQUEST_UPLOAD_IMG} {URI selected IMAGEURI = data.getData (); UploadImgVW.setImageURI (selectedImageURI); } Else {Toast.makeText (MainActivity.This is, "You can select only one image.", Toast.LNGTH_LONG). Show (); }}}   

But if the user selects a size with a higher size (such as 2MB size), then the application exited the following error, but it Very well with normal (KB level) images and wondering what I can do for this issue (to handle this error situation). Thank you ...

Error - & gt;

  06-20 16: 43: 58.445: E / Android Random (2075): Fatal Exceptions: Key 06-20 16: 43: 58.445: E / Android Random (2075): java. Lang.OutOfMemoryError: bitmap size exceeds VM budget    

A series of such articles Which are efficiently describe. Looking at your code, you load the image without knowing it how big it is and you will eventually face these problems, especially if you load and process many images.

The idea described in it is already scaling down the bitsmap (first you check how large the image is to load, then you calculate the down scaling factor and only after you scale down -Image will load). For this, you have to first know the dimensions of ImageView, then you have to use it because you have one of the target files to display. The file should be trivial to the Uri.

In addition, you have to check your app as well as memory consumption ... you have other resources which are hanging in memory and you need to clean them. I have a very useful tool I'm using - a very good article on this, author, Patrick Duboy, organized a very interesting session in Google IO 2011 on this topic. , It was very useful for me ...

No comments:

Post a Comment