【问题标题】:Leak of memory with a rescale of bitmap位图重新缩放导致内存泄漏
【发布时间】:2014-09-22 11:50:32
【问题描述】:

我想缩放图像以使它们保持相同的比例。因此,例如,箭头在重新缩放后的所有图像中具有相同的大小。所以我关注了this example,它工作正常。 但是在失去对列表视图的操作之后,我可能会遇到OutOfMemoryError 错误。我在 DDMS 中检查堆转储,没错,分配大小总是增加。我放了一些bitmap.recycle(),但它会导致错误:"cannot draw recycled bitmaps"

我也试过the official tutorial,但是我遇到了问题,下载的样本和解释的不一样,我也不是很懂。

请问,您能解释一下如何解决我的错误吗? 谢谢

【问题讨论】:

  • “一些 bitmap.recycle()”是什么意思?那里应该,我的意思是¡¡是每个已处理的位图实例的回收。除非,内存消耗会越来越高。而且,如果用户继续太快地触发重新缩放,即使您将它们全部处理掉,也没有理由不继续上升。

标签: android bitmap out-of-memory scale


【解决方案1】:

Bitmaps 始终存储在设备 Heap Memory 中,而您的设备 heap memory 负担不起Bitmaps 的数量,这可能是您尝试的方式在图像的相同分辨率下工作,所以,首先尝试使用您的应用清单 grow the heap,然后尝试通过这种方式调整您的 Bitmaps

public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {

        if(bitmapToScale == null)
            return null;
        //get the original width and height
        int width = bitmapToScale.getWidth();
        int height = bitmapToScale.getHeight();
        // create a matrix for the manipulation
        Matrix matrix = new Matrix();

        // resize the bit map
        matrix.postScale(newWidth / width, newHeight / height);

        // recreate the new Bitmap and set it back
        return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);  

    }

如果这也不起作用,那么,您必须执行以下操作之一:

  1. 为每张图片分配的宽度和高度要小得多。
  2. 使用ImageLoader之类的方式压缩图像。
  3. 使用具有大量堆空间的其他设备。
  4. 限制使用的图片数量。

提示: 回收 Bitmap 会将其从堆内存中完全删除,因此,出现 "cannot draw recycled bitmaps" 错误是合乎逻辑的,因此,如果您想 recycle 它然后将其用于您的应用程序,您必须首先将其存储在 ArrayList<Bitmaps> 之类的位置。

【讨论】:

  • 我已经在清单中增加了堆,您的解决方案很困难,因为我不知道图像的最终大小。感谢您的回答
猜你喜欢
  • 2016-03-14
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
相关资源
最近更新 更多