2011-10-01 47 views
4

我知道這可能已被解決,但是,我的Android應用程序中有內存泄漏問題。每次按下按鈕時,我都會通過用戶庫中的不同圖片進行循環。這對第一對夫婦工作正常,然後引發內存不足異常。我環顧四周,雖然我明白,即使沒有指向圖片,照片仍然存儲在堆(?)上。有沒有辦法我可以強制這個清理,所以我沒有得到錯誤?我試過以下......不使用靜態圖像時避免Android內存泄漏

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

更新::更新這工作!

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

感謝

回答

4

你可以使用Bitmap.recycle()如果你可以用位圖改變你的繪製對象。