我知道这可能已经解决了,但是,我的 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));
}

谢谢

最佳答案

如果您可以使用位图更改 Drawable,则可以使用 Bitmap.recycle()

关于android - 不使用静态图像时避免 Android 内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7621502/

10-12 03:28