开发者

Avoid Android Memory Leak when not using Static Image

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜