java.lang.OutOfMemoryError:无法分配带有16777216个空闲字节的573308940字节分配和直到OOM的215MB

时间:2018-06-17 10:07:56

标签: android android-bitmap

我尝试了一些修复,但它们无法正常工作,我尝试在AndroidManifest.xml文件中添加以下两行代码:

机器人:largeHeap ="真"
机器人:硬件加速="假"

我正在添加错误列表:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.dubey.happybirthday, PID: 26683
              java.lang.OutOfMemoryError: Failed to allocate a 573308940 byte allocation with 16777216 free bytes and 215MB until OOM
                  at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
                  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
                  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155)
                  at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
                  at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
                  at android.content.res.Resources.loadDrawable(Resources.java:858)
                  at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
                  at android.widget.ImageView.<init>(ImageView.java:162)
                  at android.widget.ImageView.<init>(ImageView.java:150)
                  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:71)
                  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:67)
                  at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:181)
                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105)
                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                  at com.example.dubey.happybirthday.MainActivity.onCreate(MainActivity.java:11)
                  at android.app.Activity.performCreate(Activity.java:6684)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6186)
                  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)

1 个答案:

答案 0 :(得分:0)

使用像Picasso或Glide这样的图像加载库。如果你的图像确实有573 MB(如果你直接在你的代码中保留Bitmap或Drawable实例也可能是内存泄漏)尝试扩展它(我认为你不需要这么大的图像。在大多数情况下,图像与低于1920x1080的分辨率在手机上看起来很棒)并将其转换为jpg或png。