Android 内存不足错误

Android OutOfMemoryError

在我的应用程序中,我有 20 多张图片是布局背景。当我添加另一张图片时,应用程序已停止工作。

01-20 22:01:51.707: E/AndroidRuntime(15045): FATAL EXCEPTION: main
01-20 22:01:51.707: E/AndroidRuntime(15045): Process: xxxxxx, PID: 15045
01-20 22:01:51.707: E/AndroidRuntime(15045): java.lang.OutOfMemoryError
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.content.res.Resources.loadDrawable(Resources.java:3056)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.content.res.Resources.getDrawable(Resources.java:1613)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at com.android.internal.widget.ActionBarView.setIcon(ActionBarView.java:1016)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at com.android.internal.policy.impl.PhoneWindow.setDefaultIcon(PhoneWindow.java:1543)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.Activity.initActionBar(Activity.java:1996)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.Activity.setContentView(Activity.java:2011)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at xxxxxx.onCreate(xxxxx.java:1338)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.Activity.performCreate(Activity.java:5426)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread.access0(ActivityThread.java:161)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.os.Looper.loop(Looper.java:157)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread.main(ActivityThread.java:5356)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at java.lang.reflect.Method.invoke(Method.java:515)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at dalvik.system.NativeStart.main(Native Method)

在方法onCreate().

中同时加载图片

main.xml:

<LinearLayout
    android:id="@+id/linearLayout27"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/abc"
    android:orientation="vertical" >

onCreate():

layout26 = (LinearLayout) findViewById(R.id.linearLayout27);
layout26.setOnClickListener(this);

我应该补充一点,每张图片的大小约为 400 x 800 像素。

我知道这个问题已经出现在这里,但我找不到适合我的情况的解决方案。

使用 android 位图时,这是一个非常常见的问题。我通常添加这个

android:largeHeap=“true”

在清单文件中,但最近我一直在使用 Picasso jar,这在处理图像时非常好。它也默认提供缓存。这是一个 link 让你开始 http://square.github.io/picasso/