我使用 JPG 背景作为我活动的根视图,并将其放在drawable
文件夹中。一切都很好,直到我决定为纵向和横向使用 2 种不同的壁纸。所以,我把第二张壁纸放在drawable-land
文件夹里。
现在的问题是:当我在真实设备上测试应用程序时,每次我改变方向两次(从纵向到横向,然后从横向到纵向),应用程序崩溃并且我看到一个 loooooong 错误日志,关于OutOfMemoryException
:
09-08 14:13:21.567: E/dalvikvm-heap(16536): Out of memory on a 16384016-byte allocation.
09-08 14:13:21.567: I/dalvikvm(16536): "main" prio=5 tid=1 RUNNABLE
09-08 14:13:21.567: I/dalvikvm(16536): | group="main" sCount=0 dsCount=0 obj=0x40ad2478 self=0x149a9d0
09-08 14:13:21.567: I/dalvikvm(16536): | sysTid=16536 nice=0 sched=0/0 cgrp=default handle=1074488712
09-08 14:13:21.567: I/dalvikvm(16536): | schedstat=( 0 0 0 ) utm=346 stm=52 core=1
09-08 14:13:21.567: I/dalvikvm(16536): at android.graphics.Bitmap.nativeCreate(Native Method)
09-08 14:13:21.567: I/dalvikvm(16536): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
09-08 14:13:21.567: I/dalvikvm(16536): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-08 14:13:21.567: I/dalvikvm(16536): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
09-08 14:13:21.567: I/dalvikvm(16536): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:530)
09-08 14:13:21.567: I/dalvikvm(16536): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:505)
09-08 14:13:21.567: I/dalvikvm(16536): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:357)
09-08 14:13:21.567: I/dalvikvm(16536): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:777)
09-08 14:13:21.567: I/dalvikvm(16536): at android.content.res.Resources.loadDrawable(Resources.java:1940)
09-08 14:13:21.567: I/dalvikvm(16536): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-08 14:13:21.567: I/dalvikvm(16536): at android.view.View.<init>(View.java:2844)
09-08 14:13:21.567: I/dalvikvm(16536): at android.view.View.<init>(View.java:2781)
09-08 14:13:21.567: I/dalvikvm(16536): at android.view.ViewGroup.<init>(ViewGroup.java:412)
09-08 14:13:21.577: I/dalvikvm(16536): at android.widget.RelativeLayout.<init>(RelativeLayout.java:179)
09-08 14:13:21.577: I/dalvikvm(16536): at java.lang.reflect.Constructor.constructNative(Native Method)
09-08 14:13:21.577: I/dalvikvm(16536): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-08 14:13:21.577: I/dalvikvm(16536): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
09-08 14:13:21.577: I/dalvikvm(16536): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-08 14:13:21.577: I/dalvikvm(16536): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
09-08 14:13:21.577: I/dalvikvm(16536): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
09-08 14:13:21.577: I/dalvikvm(16536): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-08 14:13:21.577: I/dalvikvm(16536): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-08 14:13:21.577: I/dalvikvm(16536): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-08 14:13:21.577: I/dalvikvm(16536): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.Activity.setContentView(Activity.java:1835)
09-08 14:13:21.577: I/dalvikvm(16536): at com.test.IndexActivity.onCreate(IndexActivity.java:58)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.Activity.performCreate(Activity.java:4465)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3371)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.ActivityThread.access$700(ActivityThread.java:127)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
09-08 14:13:21.577: I/dalvikvm(16536): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 14:13:21.577: I/dalvikvm(16536): at android.os.Looper.loop(Looper.java:137)
09-08 14:13:21.577: I/dalvikvm(16536): at android.app.ActivityThread.main(ActivityThread.java:4441)
09-08 14:13:21.577: I/dalvikvm(16536): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 14:13:21.577: I/dalvikvm(16536): at java.lang.reflect.Method.invoke(Method.java:511)
09-08 14:13:21.577: I/dalvikvm(16536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
09-08 14:13:21.577: I/dalvikvm(16536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
09-08 14:13:21.577: I/dalvikvm(16536): at dalvik.system.NativeStart.main(Native Method)
09-08 14:13:21.577: D/AndroidRuntime(16536): Shutting down VM
09-08 14:13:21.577: W/dalvikvm(16536): threadid=1: thread exiting with uncaught exception (group=0x40ad1210)
09-08 14:13:21.597: E/AndroidRuntime(16536): FATAL EXCEPTION: main
09-08 14:13:21.597: E/AndroidRuntime(16536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.IndexActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3371)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.ActivityThread.access$700(ActivityThread.java:127)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.os.Looper.loop(Looper.java:137)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.ActivityThread.main(ActivityThread.java:4441)
09-08 14:13:21.597: E/AndroidRuntime(16536): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 14:13:21.597: E/AndroidRuntime(16536): at java.lang.reflect.Method.invoke(Method.java:511)
09-08 14:13:21.597: E/AndroidRuntime(16536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
09-08 14:13:21.597: E/AndroidRuntime(16536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
09-08 14:13:21.597: E/AndroidRuntime(16536): at dalvik.system.NativeStart.main(Native Method)
09-08 14:13:21.597: E/AndroidRuntime(16536): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
09-08 14:13:21.597: E/AndroidRuntime(16536): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-08 14:13:21.597: E/AndroidRuntime(16536): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.Activity.setContentView(Activity.java:1835)
09-08 14:13:21.597: E/AndroidRuntime(16536): at com.rahpou.android.parnian.IndexActivity.onCreate(IndexActivity.java:58)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.Activity.performCreate(Activity.java:4465)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
09-08 14:13:21.597: E/AndroidRuntime(16536): ... 12 more
09-08 14:13:21.597: E/AndroidRuntime(16536): Caused by: java.lang.reflect.InvocationTargetException
09-08 14:13:21.597: E/AndroidRuntime(16536): at java.lang.reflect.Constructor.constructNative(Native Method)
09-08 14:13:21.597: E/AndroidRuntime(16536): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
09-08 14:13:21.597: E/AndroidRuntime(16536): ... 24 more
09-08 14:13:21.597: E/AndroidRuntime(16536): Caused by: java.lang.OutOfMemoryError
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.graphics.Bitmap.nativeCreate(Native Method)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:530)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:505)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:357)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:777)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.content.res.Resources.loadDrawable(Resources.java:1940)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.View.<init>(View.java:2844)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.View.<init>(View.java:2781)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.view.ViewGroup.<init>(ViewGroup.java:412)
09-08 14:13:21.597: E/AndroidRuntime(16536): at android.widget.RelativeLayout.<init>(RelativeLayout.java:179)
09-08 14:13:21.597: E/AndroidRuntime(16536): ... 27 more
似乎加载另一个可绘制对象的成本太高了,但我已经看到许多应用程序使用大而多样的图形和壁纸并且没有问题。那么,如何防止这种内存泄漏并帮助 android 回收/销毁以前的背景可绘制对象以避免 OutOfMemory?!
如果重要的话,我的手机屏幕和壁纸都有高清(720p)分辨率,两个壁纸(港口和陆地)的大小约为 100 KB。
编辑:我尝试了第一个答案并在我的代码中对其进行了修改,因为我有一个ScrollView
作为根视图并且壁纸设置为其背景可绘制。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ScrollView root = (ScrollView)findViewById(R.id.root);
// this line causes NullPointerException:
BitmapDrawable drawable = (BitmapDrawable) root.getBackground();
Bitmap bitmap = drawable.getBitmap();
if (bitmap != null)
{
bitmap.recycle();
}
}
但事情变得最糟糕,我NullPointerException
在代码中指定的行上进行了第一次方向更改。
另一件事是为什么需要分配 16 MB 内存来创建高清位图?!我认为 1280 像素 x 800 像素 x 8(字节/像素)= 8 MB 那么为什么需要 16 MB?!(我指的是 LogCat 的第一行,它说“在 16384016 字节分配上内存不足。”和 16384016 字节 ~ 16 MB。