在创建位图时抛出OutOfMemoryError

时间:2018-12-17 09:40:21

标签: android bitmap out-of-memory

我有一个带有LCD屏幕的自定义Android设备。它可以绘制位图。我是从Android视图中准备的。

private Bitmap loadBitmapFromView() {
    if ((mView.getVisibility() != VISIBLE) ||
            (mView.getMeasuredWidth() <= 0) ||
            (mView.getMeasuredHeight() <= 0)) return null;
    Bitmap b;
    if ((mView instanceof ImageView) && !(mView instanceof ImageButton)) {
        BitmapDrawable drawable = ((BitmapDrawable) ((ImageView) mView).getDrawable());
        if (drawable == null) return null;
        b = Bitmap.createScaledBitmap(drawable.getBitmap().copy(drawable.getBitmap().getConfig(), true),
                mView.getMeasuredWidth(), mView.getMeasuredHeight(), false);
    } else {
        b = Bitmap.createBitmap(mView.getMeasuredWidth(), mView.getMeasuredHeight(), ARGB_8888);
    }
    replaceColor(b, ColorChannel.ALPHA, 0, mBackgroundColor);
    Canvas c = new Canvas(b);
    mView.layout(mLocation.x - mParentLocation.x, mLocation.y - mParentLocation.y,
            mLocation.x - mParentLocation.x + mView.getMeasuredWidth(),
            mLocation.y - mParentLocation.y + mView.getMeasuredHeight());
    mView.draw(c);
    return b;
}

此处使用ARGB_8888是因为某些视图具有透明度。

使用一段时间后,它停止绘制。

我在清单上添加了android:largeHeap="true"android:hardwareAccelerated="false"并获得了OOM

12-17 11:19:32.591 15169-15169/com.appcard.androidterminal E/art: Throwing OutOfMemoryError "Failed to allocate a 614412 byte allocation with 4194304 free bytes and 244MB until OOM"
12-17 11:19:32.619 15169-15169/com.appcard.androidterminal E/Surface: dequeueBuffer failed (Function not implemented)
12-17 11:19:32.620 15169-15169/com.appcard.androidterminal E/ViewRootImpl: Could not lock surface
    java.lang.IllegalArgumentException
        at android.view.Surface.nativeLockCanvas(Native Method)
        at android.view.Surface.lockCanvas(Surface.java:264)
        at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2998)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2966)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2753)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2367)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6598)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
        at android.view.Choreographer.doCallbacks(Choreographer.java:603)
        at android.view.Choreographer.doFrame(Choreographer.java:572)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5643)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我试图在绘制后回收位图,但这无济于事。

3 个答案:

答案 0 :(得分:1)

这是由于内存限制。您可以加载按比例缩小的位图。

请按照此处的文档了解有关如何有效加载位图的更多信息

https://developer.android.com/topic/performance/graphics/load-bitmap

答案 1 :(得分:0)

我通过用Glide替换imageView.setImageBitmap(bitmap)imageView.setImageResource(imageRes)解决了内存不足的问题: https://bumptech.github.io/glide/doc/targets.html

答案 2 :(得分:0)

尝试使用

Bitmap bitmap = Bitmap.createScaledBitmap(view.getDrawingCache(), width, height, false);

此处视图是您要绘制的视图,宽度和高度是输出的宽度和高度。

如果问题仍然存在,则可以将位图的大小减小2倍左右。其次,如果要处理多个位图,则可以一一进行,这样内存中真正需要的只有一两个位图。