android - Changing ImageView content causes OutOfMemoryError -
I have a very simple application with an image view and a button loaded by my ImageView The first drawable resource made is specified in the XML layout with the tag "Android: src", although at the runtime I want to change the image it displays . To do this, I start an activity for the result of taking an image from the SD card (intention sent to MediaStore, Picture Media, EXTERNAL_CONTENT_URI). But when the picture is selected, I try to update ImageView with the URI of the selected image, but the message " java.lang.OutOfMemoryError: bitmap size over VM budget " Is received
I'am trying to load the pictures taken with my HTC-Hero camera (picture size is approximately 1.1 m) but no success, only pictures of less than 500KB Seems to work together Still I need to load the photos taken with the camera. How can I solve it? What I'm doing wrong I think the code is very simple and should work.
Click on Public Zero (see V) {intent selectImageIntent = new intent (Intent.ACTION_PICK, android.provider.MediaStore.Images.Media .EXTERNAL_CONTENT_URI); StartActivityForResult (selectImageIntent, 1); } @ Override Protective Result on Intact Result (Int RequestCode, Int Results Results, Intent Data) {super.onActivityResult (requestCode, resultCode, Data); If (resultCode == activity. RESULT_OK) {Ury selected IMageUri = data.getData (); Logs. I (tag, "selected image:" + selected image Yuri .stustring ()); ImageView imageView = (ImageView) this.findViewById (R.id.imageView01); ImageView.setImageURI (Selected Image Yuri); // Here I get an OutofMemory error imageView.invalidate (); } Other {// Cancel}}
PS This is the only thing that the app should do, I am not making other objects, so I would like to point out that I am not using Hep Space for things other than displaying the image.
It looks like I had to recycle the bitmap displayed by the image view before loading a new bitmap Now it is working without a problem. Hope it helps someone else, just call the follwing method before setting the content of the new image view.
(BitmapDableable) imageView.getDrawable ()). GetBitmap (). Recycle ();
Then the code looks like this:
@Override Secure Zero on ActivateTilt (Int Requestcode, Inc Results, Results, Intent Data) {Super .onActivityResult (requestCode, resultcode, data); Switch (requestCode) {case INTENT_REQUEST_SELECT_IMAGE: if (resultCode == activity. RESULT_OK) {URE selected IMageUri = data.getData (); Log.i (tag, "selectedImageUri.getPath ()" + selectedImageUri.getPath ()); ImageView imageView = (ImageView) this.findViewById (R.id.ImageView_of_text); (BitmapDrawable) imageView.getDrawable ()) getBitmap () recycle () .. ImageView.setImageURI (selectedImageUri); ImageView.invalidate (); } Else {// TODO determines what the user has to do in case of breaking OCR or any other event); Default: Break; }}
Please call to recycle the ImageView bitmap.
Comments
Post a Comment