繁体   English   中英

位图缩放后的内存泄漏

[英]Leak of memory with a rescale of bitmap

我想缩放图像以使它们保持相同的比例。 因此,例如,重新缩放后,箭头在所有图像中的大小相同。 因此,我遵循了这个示例 ,并且效果很好。 但是在失去对列表视图的操作之后,我可能会遇到OutOfMemoryError错误。 我在DDMS中进行堆转储,这是正确的,分配大小始终会增加。 我放了一些bitmap.recycle()但它导致一个错误: "cannot draw recycled bitmaps"

我也尝试了官方教程 ,但是遇到了问题,下载的示例与所解释的示例不同,并且我不了解所有内容。

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

Bitmaps始终存储在设备的“ 堆内存”中 ,而设备的堆内存负担不起该数量的Bitmaps ,这可能是您尝试以相同分辨率的图像工作的方式, 因此 ,首先,请尝试使用您的应用来增加堆清单,然后尝试使用这种方法来调整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. 限制使用的图像数量。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM