1

Here is what I am doing

ImageView image2 = (ImageView) findViewById(R.id.imageView3);
image2.getDrawable().setCallback(null);
image2.setImageResource(R.drawable.fire);

where fire is an xml file proposing an animation drawable

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="true">
<item android:drawable="@drawable/fire1" android:duration="50" />
<item android:drawable="@drawable/fire2" android:duration="50" />
<item android:drawable="@drawable/fire3" android:duration="50" />
<item android:drawable="@drawable/fire4" android:duration="50" />
<item android:drawable="@drawable/fire5" android:duration="50" />
...
<item android:drawable="@drawable/fire16" android:duration="50" />

I have this error

02-12 11:37:20.919: E/dalvikvm-heap(3193): 1535664-byte external allocation too large for this process.
02-12 11:37:20.959: E/GraphicsJNI(3193): VM won't let us allocate 1535664 bytes
02-12 11:37:20.959: W/dalvikvm(3193): threadid=1: thread exiting with uncaught exception (group=0x40018560)
02-12 11:37:20.979: E/AndroidRuntime(3193): FATAL EXCEPTION: main
02-12 11:37:20.979: E/AndroidRuntime(3193): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.content.res.Resources.getDrawable(Resources.java:581)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:267)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.content.res.Resources.getDrawable(Resources.java:581)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.widget.ImageView.resolveUri(ImageView.java:501)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.widget.ImageView.setImageResource(ImageView.java:280)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at com.example.kersplatt.GameActivity.onCreate(GameActivity.java:71)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.os.Looper.loop(Looper.java:130)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at java.lang.reflect.Method.invoke(Method.java:507)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at dalvik.system.NativeStart.main(Native Method)

I read so many threads about memory leaks, memory errors (for instance java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android) but I didn't succeed in solving my problem

When I remplace image2.setImageResource(R.drawable.fire); by image2.setImageResource(R.drawable.fire1); where fire1 is a simple png, everything works fine What could I do to solve my problem

Community
  • 1
  • 1
morg
  • 1,173
  • 4
  • 18
  • 36
  • 1
    To animate view, you should use "FrameAnimation" in which your all items added as frames and put that animation to ImageView. – Chintan Rathod Feb 12 '13 at 12:19
  • And what could I do so that the images I add (with addFrame) have the same dimensions as the image originally in imageview. When I do image2.getLayoutParams().height=FrameLayout.LayoutParams.WRAP_CONTENT; image2.getLayoutParams().width=FrameLayout.LayoutParams.FILL_PARENT; (initial conditions of the imageview) it doesn't work – morg Feb 12 '13 at 14:43
  • Ok I resize using fireAnimation.addFrame(new BitmapDrawable(decodeSampledBitmapFromResource(getResources(), R.drawable.fire1, 200, 200)),50); from the like you gave, it workeds thanks ! – morg Feb 12 '13 at 16:29
  • please accept answer if it works fine for you. thanks. I have posted answer. – Chintan Rathod Feb 13 '13 at 03:20

1 Answers1

1

To remove OutOfMemory error, you should focus on scaling. You can find at http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93