位图中心裁剪中的内存不足错误

时间:2018-08-18 14:46:18

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

我试图对我的位图图像进行居中裁剪,但是一段时间后在我的后续函数中它抛出OOM错误,结果应用程序崩溃了。我可以在以下功能中进行哪些更改以处理中心作物?

private Bitmap cropBitmap(Bitmap bitmap) {
    Bitmap bmp;
    if (bitmap.getWidth() >= bitmap.getHeight()) {
        bmp = Bitmap.createBitmap(
                bitmap,
                bitmap.getWidth() / 2 - bitmap.getHeight() / 2,
                0,
                bitmap.getHeight(), bitmap.getHeight());
    } else {
        bmp = Bitmap.createBitmap(
                bitmap,
                0,
                bitmap.getHeight() / 2 - bitmap.getWidth() / 2,
                bitmap.getWidth(), bitmap.getWidth());
    }
    return bmp;
}

这是我的错误日志

08-18 20:14:15.270 2974-2974 / entertainment.minersinc.tfhy.dev D / AndroidRuntime:关闭VM 08-18 20:14:15.272 2974-2974 / entertainment.minersinc.tfhy.dev E / AndroidRuntime:FATAL EXCEPTION:main     流程:entertainment.minersinc.tfhy.dev,PID:2974     java.lang.OutOfMemoryError:未能分配12124336字节分配,其中包含2126672可用字节和2MB,直到OOM         在dalvik.system.VMRuntime.newNonMovableArray(本机方法)         在android.graphics.Bitmap.nativeCreate(本地方法)         在android.graphics.Bitmap.createBitmap(Bitmap.java:905)         在android.graphics.Bitmap.createBitmap(Bitmap.java:882)         在android.graphics.Bitmap.createBitmap(Bitmap.java:802)         在android.graphics.Bitmap.createBitmap(Bitmap.java:727)         在entertainment.minersinc.tfhy.customviews.CircularImageView.cropBitmap(CircularImageView.java:206)         在entertainment.minersinc.tfhy.customviews.CircularImageView.updateShader(CircularImageView.java:188)         在entertainment.minersinc.tfhy.customviews.CircularImageView.loadBitmap(CircularImageView.java:159)         在entertainment.minersinc.tfhy.customviews.CircularImageView.onDraw(CircularImageView.java:128)         在android.view.View.draw(View.java:17201)         在android.view.View.updateDisplayListIfDirty(View.java:16183)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.updateDisplayListIfDirty(View.java:16178)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.draw(View.java:17204)         在android.view.View.updateDisplayListIfDirty(View.java:16183)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:4774)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.draw(View.java:17204)         在android.support.v7.widget.RecyclerView.draw(RecyclerView.java:4173)         在android.view.View.updateDisplayListIfDirty(View.java:16183)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.draw(View.java:17204)         在android.view.View.updateDisplayListIfDirty(View.java:16183)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.draw(View.java:17204)         在android.support.v4.widget.NestedScrollView.draw(NestedScrollView.java:1888)         在android.view.View.updateDisplayListIfDirty(View.java:16183)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.draw(View.java:17204)         在android.view.View.updateDisplayListIfDirty(View.java:16183)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.updateDisplayListIfDirty(View.java:16178)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1246)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.draw(View.java:17204)         在android.view.View.updateDisplayListIfDirty(View.java:16183)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.updateDisplayListIfDirty(View.java:16178)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.updateDisplayListIfDirty(View.java:16178)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.updateDisplayListIfDirty(View.java:16178)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.updateDisplayListIfDirty(View.java:16178)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.updateDisplayListIfDirty(View.java:16178)         在android.view.View.draw(View.java:16967)         在android.view.ViewGroup.drawChild(ViewGroup.java:3731)         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:3517)         在android.view.View.draw(View.java:17204)         在com.android.internal.policy.DecorView.draw(DecorView.java:753)         在android.view.View.updateDisplayListIfDirty(View.java:16183)         在android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:648)         在android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:654)         在android.view.ThreadedRenderer.draw(ThreadedRenderer.java:762)         在android.view.ViewRootImpl.draw(ViewRootImpl.java:2818)         在android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2626)         在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2233)         在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1272)         在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6408)         在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:874)         在android.view.Choreographer.doCallbacks(Choreographer.java:686)         在android.view.Choreographer.doFrame(Choreographer.java:621)         在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:860)         在android.os.Handler.handleCallback(Handler.java:751)         在android.os.Handler.dispatchMessage(Handler.java:95)         在android.os.Looper.loop(Looper.java:154)         在android.app.ActivityThread.main(ActivityThread.java:6165)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

1 个答案:

答案 0 :(得分:0)

您的位图太大。这是显而易见的。
在清单中的android:largeHeap="true"标签下尝试application
也许会起作用。