加载一个图像时出现内存不足错误

时间:2018-01-06 18:10:19

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

在活动中加载图片时出现OutOfMemmory错误。 我有一个活动与一个图像,就是这样。我不明白一个图像如何导致此错误

日志:

java.lang.OutOfMemoryError: Failed to allocate a 144288012 byte allocation with 2126212 free bytes and 92MB 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:72)
                                                 at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
                                                 at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                 at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
                                                 at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
                                                 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 in.snipped.snippedsalons.MainActivity.onCreate(MainActivity.kt:10)
                                                 at android.app.Activity.performCreate(Activity.java:6679)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:154)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:0)

请在清单申请中添加遗漏

<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

您可以找到更多here

答案 1 :(得分:0)

有两种方法可以摆脱这个错误

  1. 压缩图像并根据需要更改分辨率。
  2. 使用滑动来加载图像,根据图像视图的大小滑动制作该图像的副本
  3. 你可以尝试两者。