开发者

Avoid Android Memory Leak when not using Static Image

开发者 https://www.devze.com 2023-04-09 15:36 出处:网络
I know this has probably been addressed, however, I am having memory leak issues in my Android app. I have it cycle through a differant picture in the users library every time they push a button. This

I know this has probably been addressed, however, I am having memory leak issues in my Android app. I have it cycle through a differant picture in the users library every time they push a button. This works fine for the first couple then throws an out of memory exception. I have looked around and though I understood that the pictures are being stored on the heap(?) even after it is not being pointed at. Is there a way I can force this to clean up so I am not getting the error? I have tried the following....

private void setImage() throws RemoteException{
    view.setBackgroundDrawable(null);
    currentB开发者_JS百科ackground = 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消