Android OutOfMemory错误ImageView

时间:2015-10-02 20:14:33

标签: android memory

  java.lang.OutOfMemoryError: Failed to allocate a 14400012 byte allocation with 5645520 free bytes and 5MB until OOM
            at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:726)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:547)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014)
            at android.content.res.Resources.loadDrawableForCookie(Resources.java:3730)
            at android.content.res.Resources.loadDrawable(Resources.java:3603)
            at android.content.res.Resources.getDrawable(Resources.java:1852)
            at android.content.Context.getDrawable(Context.java:408)
            at android.view.View.setBackgroundResource(View.java:17228)
            at alexcz.shapetest.Test.nextQ(Test.java:89)
            at alexcz.shapetest.Test$1.onClick(Test.java:54)

错误^^

触发任何onClick事件时会发生错误。所有其他图像显示除了这些2.这两个图像都不大,大约600x600。在代码中,错误发生在

nextq.setBackgroundResource(R.drawable.nextq);
result.setBackgroundResource(R.drawable.correct); 

提前致谢。

GitHub的:https://github.com/alex578344/ShapeTestLogic

2 个答案:

答案 0 :(得分:1)

android:largeHeap ="true"

不是灵丹妙药,也是最新的优化方法。浏览this official guide了解详情。

我建议将ImageView替换为subsampling-scale-image-view。它是一个非常方便快捷的库,具有兼容的ImageView接口。它会将图片分批发送,排除此异常。

答案 1 :(得分:-1)

将largeHeap放入AndroidManifest.xml

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