Coderji
Coderji

Reputation: 7745

OutOfMemory error android

I am developing an android game. it is almost finished and tried it in 10.1 tablets, nexus 7, nexus 4, HTC senstation it worked on all of them. However, when I tried it on Galaxy s3 I got OutofMemory error when Im calling pics in frame animation xml. now I solved this problem by resizing the pics in xhdpi folder but the results wasn't satisfying. Is there any sufficient method I can use to rescale pics that are displayed in anim to do a frame animation.

Here is the code:

public class Logo extends Activity {

    private ImageView iv;
    Thread timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_logo);

        iv = (ImageView) findViewById(R.id.imageView1);
        iv.setBackgroundResource(R.anim.anime);

        // thread for animating the logo and moving to next activity after
        // display.
        timer = new Thread() {

            @Override
            public void run() {
                try {
                    sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //Intent i = new Intent();
                    //i.setClass(Logo.this, StartPoint.class);
                    //startActivity(i);
                }
            }
        };
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        AnimationDrawable anime = (AnimationDrawable) iv.getBackground();
        if (hasFocus) {
            anime.start();
            timer.start();
        } else {
            anime.stop();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.logo, menu);
        return true;
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        finish(); // to remove this activity.
    }
}

here is the anime.xml:

<?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/a1" android:duration="60" />
<item android:drawable="@drawable/a2" android:duration="60" />
<item android:drawable="@drawable/a3" android:duration="60" />
<item android:drawable="@drawable/a4" android:duration="60" />
<item android:drawable="@drawable/a5" android:duration="60" />
<item android:drawable="@drawable/a6" android:duration="60" />
<item android:drawable="@drawable/a7" android:duration="60" />
<item android:drawable="@drawable/a8" android:duration="60" />
<item android:drawable="@drawable/a9" android:duration="60" />
<item android:drawable="@drawable/a10" android:duration="60" />
<item android:drawable="@drawable/a11" android:duration="60" />
<item android:drawable="@drawable/a12" android:duration="60" />
<item android:drawable="@drawable/a13" android:duration="60" />
<item android:drawable="@drawable/a14" android:duration="60" />
<item android:drawable="@drawable/a15" android:duration="60" />
<item android:drawable="@drawable/a16" android:duration="60" />
<item android:drawable="@drawable/a17" android:duration="60" />
<item android:drawable="@drawable/a18" android:duration="60" />
<item android:drawable="@drawable/a19" android:duration="60" />
<item android:drawable="@drawable/a20" android:duration="60" />
<item android:drawable="@drawable/a21" android:duration="60" />
<item android:drawable="@drawable/a22" android:duration="60" />
<item android:drawable="@drawable/a23" android:duration="60" />
<item android:drawable="@drawable/a24" android:duration="60" />
<item android:drawable="@drawable/a25" android:duration="60" />
<item android:drawable="@drawable/a26" android:duration="60" />
<item android:drawable="@drawable/a27" android:duration="60" />

</animation-list>

Now I saw many answers that illustrates bitmapFactory.options but how can I apply these options on xml file and if it isnt possible. is there any other methods I can use instead of resizing the pics. Thank you in advance.

here I tried to apply the code programmaticaly and still got the same error:

//onCreate and thread then 
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);

    BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.a1);
    BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.a2);
    BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.a3);
    BitmapDrawable frame4 = (BitmapDrawable)getResources().getDrawable(R.drawable.a4);
    BitmapDrawable frame5 = (BitmapDrawable)getResources().getDrawable(R.drawable.a5);
    BitmapDrawable frame6 = (BitmapDrawable)getResources().getDrawable(R.drawable.a6);
    BitmapDrawable frame7 = (BitmapDrawable)getResources().getDrawable(R.drawable.a7);
    BitmapDrawable frame8 = (BitmapDrawable)getResources().getDrawable(R.drawable.a8);
    BitmapDrawable frame9 = (BitmapDrawable)getResources().getDrawable(R.drawable.a9);
    BitmapDrawable frame10 = (BitmapDrawable)getResources().getDrawable(R.drawable.a10);
    BitmapDrawable frame11 = (BitmapDrawable)getResources().getDrawable(R.drawable.a11);
    BitmapDrawable frame12 = (BitmapDrawable)getResources().getDrawable(R.drawable.a12);
    BitmapDrawable frame13 = (BitmapDrawable)getResources().getDrawable(R.drawable.a13);
    BitmapDrawable frame14 = (BitmapDrawable)getResources().getDrawable(R.drawable.a14);
    BitmapDrawable frame15 = (BitmapDrawable)getResources().getDrawable(R.drawable.a15);
    BitmapDrawable frame16 = (BitmapDrawable)getResources().getDrawable(R.drawable.a16);
    BitmapDrawable frame17 = (BitmapDrawable)getResources().getDrawable(R.drawable.a17);
    BitmapDrawable frame18 = (BitmapDrawable)getResources().getDrawable(R.drawable.a18);
    BitmapDrawable frame19 = (BitmapDrawable)getResources().getDrawable(R.drawable.a19);
    BitmapDrawable frame20 = (BitmapDrawable)getResources().getDrawable(R.drawable.a20);
    BitmapDrawable frame21 = (BitmapDrawable)getResources().getDrawable(R.drawable.a21);
    BitmapDrawable frame22 = (BitmapDrawable)getResources().getDrawable(R.drawable.a22);
    BitmapDrawable frame23 = (BitmapDrawable)getResources().getDrawable(R.drawable.a23);
    BitmapDrawable frame24 = (BitmapDrawable)getResources().getDrawable(R.drawable.a24);
    BitmapDrawable frame25 = (BitmapDrawable)getResources().getDrawable(R.drawable.a25);
    BitmapDrawable frame26 = (BitmapDrawable)getResources().getDrawable(R.drawable.a26);
    BitmapDrawable frame27 = (BitmapDrawable)getResources().getDrawable(R.drawable.a27);

    final AnimationDrawable anime = new AnimationDrawable();
    anime.setOneShot(true);
    anime.addFrame(frame1, 60);
    anime.addFrame(frame2, 60);
    anime.addFrame(frame3, 60);
    anime.addFrame(frame4, 60);
    anime.addFrame(frame5, 60);
    anime.addFrame(frame6, 60);
    anime.addFrame(frame7, 60);
    anime.addFrame(frame8, 60);
    anime.addFrame(frame9, 60);
    anime.addFrame(frame10, 60);
    anime.addFrame(frame11, 60);
    anime.addFrame(frame12, 60);
    anime.addFrame(frame13, 60);
    anime.addFrame(frame14, 60);
    anime.addFrame(frame15, 60);
    anime.addFrame(frame16, 60);
    anime.addFrame(frame17, 60);
    anime.addFrame(frame18, 60);
    anime.addFrame(frame19, 60);
    anime.addFrame(frame20, 60);
    anime.addFrame(frame21, 60);
    anime.addFrame(frame22, 60);
    anime.addFrame(frame23, 60);
    anime.addFrame(frame24, 60);
    anime.addFrame(frame25, 60);
    anime.addFrame(frame26, 60);
    anime.addFrame(frame27, 60);


    iv.setBackgroundDrawable(anime);
    if (hasFocus) {
        anime.start();
        timer.start();
    }

    else
        anime.stop();
}

Upvotes: 0

Views: 254

Answers (1)

Sam Aleksov
Sam Aleksov

Reputation: 1201

The Galaxy S3 in fact has relatively a small default heap size(32MB) for its specs.

You can temporary workaround this issue requesting a larger heap and checking if this fixes your problem.

<application android:largeHeap="true">
        . . .
</application>

Then I would work on the size and type of the frame resources.

Cheers.

Upvotes: 2

Related Questions