加载动画.xml时出现OutOfMemoryError

时间:2015-02-04 09:56:46

标签: android android-animation drawable android-drawable

我有以下ImageView

imageView.setBackgroundResource(R.drawable.create_user);

我的create_user drawable看起来像这样:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">    
  <item android:drawable="@drawable/create_user_0" android:duration="500" />
  <item android:drawable="@drawable/create_user_1" android:duration="500" />
</animation-list>

但是在.xml中设置imageView.setBacgroundResource(R.drawable.create_user)时,会启动以下异常:

02-04 10:49:07.994: E/AndroidRuntime(1379): FATAL EXCEPTION: main
02-04 10:49:07.994: E/AndroidRuntime(1379): java.lang.OutOfMemoryError
02-04 10:49:07.994: E/AndroidRuntime(1379):     at  android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.content.res.Resources.loadDrawable(Resources.java:2988)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at  android.content.res.Resources.getDrawable(Resources.java:1558)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:873)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.content.res.Resources.loadDrawable(Resources.java:2970)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.content.res.Resources.getDrawable(Resources.java:1558)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.view.View.setBackgroundResource(View.java:15697)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at com.facephi.sdk.ui.FragmentCreatingStructure.onCreateView(FragmentCreatingStructu re.java:29)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.app.Fragment.performCreateView(Fragment.java:1699)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.os.Handler.handleCallback(Handler.java:730)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.os.Looper.loop(Looper.java:176)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at android.app.ActivityThread.main(ActivityThread.java:5419)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at java.lang.reflect.Method.invoke(Method.java:525)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
02-04 10:49:07.994: E/AndroidRuntime(1379):     at  dalvik.system.NativeStart.main(Native Method)

我不得不说我已经使用这个动画了很长时间。昨天是它第一次开始崩溃。

我无法想象此时发生的事情......任何人都可以帮我找到我做错的事吗?

注意:在错误发布之前显示Out of memory on a 638416-byte allocation

注意2:在应用程序崩溃之前,它也会多次说明这一点。

02-04 11:23:56.179: I/dalvikvm(7266): "Binder_1" prio=5 tid=9 RUNNABLE
02-04 11:23:56.179: I/dalvikvm(7266):   | group="main" sCount=0 dsCount=0 obj=0x427f2200 self=0x4192fcd0
02-04 11:23:56.179: I/dalvikvm(7266):   | sysTid=7277 nice=0 sched=0/0 cgrp=apps handle=1100151432
02-04 11:23:56.179: I/dalvikvm(7266):   | state=R schedstat=( 504400204 75428596 671 ) utm=34 stm=15 core=2
02-04 11:23:56.179: I/dalvikvm(7266):   at dalvik.system.NativeStart.run(Native Method)
02-04 11:23:56.179: E/Camera-JNI(7266): Couldn't allocate byte array for JPEG data
02-04 11:23:56.239: I/dalvikvm-heap(7266): Clamp target GC heap from 71.012MB to 64.000MB
02-04 11:23:56.239: D/dalvikvm(7266): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 11:23:56.239: D/dalvikvm(7266): GC_FOR_ALLOC freed 21K, 3% free 63352K/65192K, paused 17ms, total 17ms
02-04 11:23:56.239: I/dalvikvm-heap(7266): Forcing collection of SoftReferences for 460816-byte allocation
02-04 11:23:56.259: I/dalvikvm-heap(7266): Clamp target GC heap from 71.012MB to 64.000MB
02-04 11:23:56.259: D/dalvikvm(7266): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 11:23:56.259: D/dalvikvm(7266): GC_BEFORE_OOM freed 0K, 3% free 63352K/65192K, paused 20ms, total 20ms
02-04 11:23:56.259: E/dalvikvm-heap(7266): Out of memory on a 460816-byte allocation.

3 个答案:

答案 0 :(得分:1)

尝试在清单中添加 android:largeHeap =&#34; true&#34;,以查看是否可以解决此问题。

或使用 bitmapfactory 的api inSampleSize 对图片进行采样。

答案 1 :(得分:0)

我也遇到过这种错误,但是当我调整大小时,它就得到了解决。 尝试重新调整图片大小。

答案 2 :(得分:0)

您可以尝试调整图片大小以使其适应视图的确切大小,如here所述。您甚至可以尝试使用universal image loader之类的库来简化此任务。