【问题标题】:Android out of memory error while rotate a bitmap旋转位图时Android内存不足错误
【发布时间】:2017-08-19 21:21:19
【问题描述】:

我目前正在做一个项目,我想在其中旋转位图。

第一次,我使用以下代码创建位图:

myBitmap = BitmapFactory.decodeResource(getResources(), drawableResource);

然后我使用以下代码旋转位图:

final Matrix matrix = new Matrix();
matrix.postRotate(currentRotate);
myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, directionBitmap.getWidth(), directionBitmap.getHeight(), matrix, true);

我工作,但几次后,内存增加,我有以下异常:

java.lang.OutOfMemoryError: 分配 119071756 字节失败 分配 16775968 个可用字节和 96MB 直到 OOM

似乎旧的位图仍在内存中。如何删除/回收它们以节省内存?

感谢您的帮助。

【问题讨论】:

    标签: android out-of-memory android-bitmap createbitmap


    【解决方案1】:

    我可以建议你使用 Github 的 Glide 库。

    这个库在后台线程中工作。无论如何,您可以像这样在背景上执行旋转:

    runOnUiThread(new Runnable(final Matrix matrix = new Matrix();
    matrix.postRotate(currentRotate);
    myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, directionBitmap.getWidth(), directionBitmap.getHeight(), matrix, true);
    )
    

    【讨论】:

      【解决方案2】:

      就我而言,问题在于我使用的位图的大小。对于给定的设备,我使用的位图具有非常高的像素。在这种情况下,Android 系统必须对它们进行除垢以降低像素密度,即适合您设备的像素密度。当您旋转位图时,Android 系统会占用大量内存以将其缩小为较低的像素。此外,Android 会变得很忙,在某些情况下会导致 UI 线程阻塞。 此外,增加清单文件中的堆大小。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-17
        • 1970-01-01
        • 1970-01-01
        • 2012-07-21
        • 2011-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多