please help me get over java.lang.OutOfMemoryError: bitmap size exceeds VM budget
<p>i have to display 20 same balls jump at the same style at different positions. but i keep getting the  error: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
.</p>

<p>at first i tried with xml files in anim folder. but i can't release any resource declared in xml. so i changed to create Bitmap with BitmapFactory, and call recycle function. but i can't get over either.</p>

<p>the code flow is something like this:</p>

<pre><code>Bitmap[] ballBitmap;
int[] playOrder = new int[2];   //the balls will play in order of the definiens in playOrder
ImageView[] balls = new ImageView[20];  //it contains 20 balls

private void iniNewQuestion() {
passNum++;
if(passNum &gt; 1) clearPic();
if(passNum &lt; 20) playBall();
}


private void playBall() {
int ballIndex = playOrder[passNum - 1] - 1;
ballBitmap = new Bitmap[9];
ballBitmap[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_01);
ballBitmap[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_02);
ballBitmap[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_03);
ballBitmap[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_04);
ballBitmap[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_05);
ballBitmap[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_06);
ballBitmap[6] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_07);
ballBitmap[7] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_08);
ballBitmap[8] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_09);

AnimationDrawable ballAnimation = new AnimationDrawable();
for (int picIndex = 0; picIndex &lt; 9; picIndex++) 
{
    ballAnimation.addFrame(new BitmapDrawable(ballBitmap[picIndex] ), breakTime[picIndex]);
    //if(picIndex &gt; 0) ballBitmap[picIndex].recycle();
}

ballAnimation.setOneShot(true);
balls[ballIndex].setBackgroundDrawable(ballAnimation); 
ballAnimation.start();

Handler toNextQuestionHandler = new Handler();
toNextQuestionHandler.postDelayed(runIniNewQuestion, 3000);
}

private Runnable runIniNewQuestion = new Runnable() { public void run() { iniNewQuestion(); } };

private void clearPic() {
    int picIndex = 0; 
    for (picIndex = 0; picIndex &lt; 9; picIndex++) 
    {
            if(ballBitmap[picIndex] != null ) 
            {
                    if(!ballBitmap[picIndex].isRecycled()) ballBitmap[picIndex].recycle();
            }
    }
}
</code></pre>

<p>it always happens when I play the 6th ball. the error log is:</p>

<p>01-28 05:29:31.927: E/AndroidRuntime(1090): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.Bitmap.nativeCreate(Native Method)
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:346)
01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:372)
01-28 05:29:31.927: E/AndroidRuntime(1090): at com.gzconcern.animalaba.AbaGame.<strong>playBall</strong>(AbaGame.java:533)</p>

<p>I thought when I add a frame to ballAnimation, i have new a Bitmap from the picture, so I should release the picture right away. but when I do call recycle, it turns out this:</p>

<p>java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@44e85c60</p>
