JDFuzyll
JDFuzyll

Reputation: 77

OutOfMemoryError when loading animations

I have tried this : OutOfMemoryError

And read many other such posts , but none of them worked

This is the error :

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.lud.root.jetfighter, PID: 19789
                  java.lang.OutOfMemoryError: Failed to allocate a 4940652 byte allocation with 2168368 free bytes and 2MB until OOM
                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:486)
                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1085)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2879)
                      at android.content.res.Resources.loadDrawable(Resources.java:2768)
                      at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                      at android.graphics.drawable.AnimationDrawable.inflateChildElements(AnimationDrawable.java:324)
                      at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:294)
                      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1220)
                      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1129)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2874)
                      at android.content.res.Resources.loadDrawable(Resources.java:2768)
                      at android.content.res.Resources.getDrawable(Resources.java:932)
                      at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:133)
                      at android.content.Context.getDrawable(Context.java:465)
                      at android.view.View.setBackgroundResource(View.java:17566)
                      at android.support.v7.widget.AppCompatImageView.setBackgroundResource(AppCompatImageView.java:76)
                      at com.lud.root.jetfighter.Instruction.onCreate(Instruction.java:32)
                      at android.app.Activity.performCreate(Activity.java:6583)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
                      at android.os.Handler.dispatchMessage(Handler.java:111)
                      at android.os.Looper.loop(Looper.java:207)
                      at android.app.ActivityThread.main(ActivityThread.java:5769)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
I/System: FinalizerDaemon: finalize objects = 381

Reading some other posts I came to know of large bitmaps exceeding the memory limit, but the files are too small to I think exceed the limit ( they are 8 to 12kB max .png files)

I have read many of the posts referring to the bitmap problem but none could help me.

I tried this : android:largeHeap="true"

But since it didn't work , I read another article and it suggested to remove it , so I did it .

My Instuction.java file which runs into the error:

setContentView(R.layout.activity_instruction);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ll = (LinearLayout)findViewById(R.id.instruction);
ll.setOnClickListener(this);
imv1 = (ImageView) findViewById(R.id.instruction_imv1);
imv3 = (ImageView) findViewById(R.id.instruction_imv3);
imv1.setBackgroundResource(R.drawable.inst);   // **This is where error occurs**
imv3.setBackgroundResource(R.drawable.collision); 

tap = (AnimationDrawable) imv1.getBackground();
tap.start();

collision = (AnimationDrawable) imv3.getBackground();
collision.start();

I loaded 22 files for the inst animation.

This is my inst.xml file:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/enlarge001" android:duration="100" />
    <item android:drawable="@drawable/enlarge002" android:duration="100" />
    <item android:drawable="@drawable/enlarge003" android:duration="100" />
    <item android:drawable="@drawable/enlarge005" android:duration="100" />
    <item android:drawable="@drawable/enlarge006" android:duration="100" />
    <item android:drawable="@drawable/enlarge007" android:duration="100" />
    <item android:drawable="@drawable/enlarge008" android:duration="100" />
    <item android:drawable="@drawable/enlarge009" android:duration="100" />
    <item android:drawable="@drawable/enlarge010" android:duration="100" />
    <item android:drawable="@drawable/enlarge011" android:duration="100" />
    <item android:drawable="@drawable/enlarge012" android:duration="100" />
    <item android:drawable="@drawable/enlarge013" android:duration="100" />
    <item android:drawable="@drawable/enlarge014" android:duration="100" />
    <item android:drawable="@drawable/enlarge015" android:duration="100" />
    <item android:drawable="@drawable/enlarge016" android:duration="100" />
    <item android:drawable="@drawable/enlarge017" android:duration="100" />
    <item android:drawable="@drawable/enlarge018" android:duration="100" />
    <item android:drawable="@drawable/enlarge020" android:duration="100" />
    <item android:drawable="@drawable/enlarge021" android:duration="100" />
    <item android:drawable="@drawable/enlarge022" android:duration="100" />

</animation-list>

Upvotes: 0

Views: 431

Answers (2)

Christine
Christine

Reputation: 5575

An animation list loads all images before starting the animation. If that causes an OOM, you can reduce the number of images, or reduce the size of the images (pixel size, not file size), or both. Or you can create an image loader that loads the images to your ImageView on the fly. Still another solution is to create a video of your animation and show that video.

There's no easy solution I think. I have been working on an app with multiple simultaneous animations, and we were constantly struggling with getting everything to work smoothly. We ended up making compromises in resolution and image sizes, and leaving out some animations.

Upvotes: 1

Niza Siwale
Niza Siwale

Reputation: 2406

How large are the images in terms of pixels. The reason you get an outOfMemroy exception is not to do with the byte sizes of your images, but the pixel sizes. I'd recommend you use 10 of those 22 images as they only animate for 100ms and thus the user will not know the difference

Upvotes: 0

Related Questions