欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Android】解决“java.lang.OutOfMemoryError: Failed to allocate a 8015852 byte allocation with 670144 fre

程序员文章站 2022-06-13 21:29:29
...
11-28 09:42:11.459 19178-19178/com.jddw.printer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.jddw.printer, PID: 19178
    java.lang.OutOfMemoryError: Failed to allocate a 8015852 byte allocation with 670144 free bytes and 654KB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.Bitmap.nativeCreate(Native Method)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:859)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:829)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:796)
        at com.jddw.printer.MyPrintAdapter.onLayout(MyPrintAdapter.java:80)
        at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:797)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:150)
        at android.app.ActivityThread.main(ActivityThread.java:5546)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:682)

解决办法:
在AndroidManifest.xml文件中的application标签中添加如下四行代码就解决问题了

android:allowBackup="true"
android:supportsRtl="true"
android:largeHeap="true"
android:hardwareAccelerated="false"

android:hardwareAccelerated = "true"的时候会牺牲内存来加快图片的加载速度。
android:largeHeap = "false"会限制app应用申请的内存大小。

相关标签: Android