Avoid Android memory leak if not using static image

I know that this was probably addressed, however, I have problems with memory leak in the Android app. I have a loop through a different picture in the users library every time they click a button. This works fine for the first pair and then throws an exception in memory. I looked around, and although I realized that the photos are stored in a heap (?) Even after they do not point to it. Is there a way to make this clear so that I don't get an error? I tried the following ....

private void setImage() throws RemoteException{ view.setBackgroundDrawable(null); currentBackground = Drawable.createFromPath(backgroundImageService.getCurrentImageLocation()); view.setBackgroundDrawable(currentBackground); } 

UPDATE :: Update This worked !!!

 private void setImage() throws RemoteException{ if(currentBackground != null){ currentBackground.recycle(); } currentBackground = BitmapFactory.decodeFile(backgroundImageService.getCurrentImageLocation()); view.setBackgroundDrawable(new BitmapDrawable(currentBackground)); } 

thanks

+4
source share
1 answer

you can use Bitmap.recycle () if you can change your Drawable with Bitmap.

+4
source

All Articles