解决:java.lang.OutOfMemoryError: Failed to allocate a 20971532 byte allocation with 4194304 free bytes and 7MB until OOM
05-13 19:10:53.922 15103-15103/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.a14412.tvoid1, PID: 15103 java.lang.OutOfMemoryError: Failed to allocate a 20971532 byte allocation with 4194304 free bytes and 7MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:709) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:544) at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:567) at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:597) at com.example.a14412.tvoid1.pusher.ScreencaptureActivity.onCreate(ScreencaptureActivity.java:100) at android.app.Activity.performCreate(Activity.java:6336) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2676) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1510) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5786) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
AndroidManifest.xml:添加这两个,我加在了application层级
也可以加在activity级
android:largeHeap="true"
android:hardwareAccelerated="false"
<application android:name=".im.ImApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:largeHeap="true" android:hardwareAccelerated="false" android:theme="@style/AppTheme">