I prepared no.of drawable animations.when launching the first animation application will start.i there are two buttons (next and previous) with the same activity. When I click on the next button, I get an exception, for example,
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
My code
To get drawable animations using drawable,
private void getAnimationForLetters(int mAnim) { // TODO Auto-generated method stub String anim = "capital_anim_" + new Integer(mAnim).toString(); final int resID = getApplicationContext().getResources().getIdentifier( anim, "drawable", "my-package-name"); mImgLetter.clearAnimation(); mImgLetter.setBackgroundResource(resID); mImgLetter.post(new Runnable() { public void run() { loadingAnimation = (AnimationDrawable) mImgLetter .getBackground(); loadingAnimation.start(); } }); }
my next button code,
case R.id.btnNext_: unbindDrawables(findViewById(R.id.imgLetter)); mLetterNum=mLetterNum+1; getAnimationForLetters(mLetterNum);
my invind drawable method code,
private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } }
Finally, I got the java.lang.OutOfMemoryError: bitmap size exceeds VM budget exception java.lang.OutOfMemoryError: bitmap size exceeds VM budget The mImgLetter.setBackgroundResource(resID); exception is displayed here mImgLetter.setBackgroundResource(resID); Please help me.
I added the following code to clean up,
loadingAnimation.stop(); for (int i = 0; i < loadingAnimation.getNumberOfFrames(); ++i){ Drawable frame = loadingAnimation.getFrame(i); if (frame instanceof BitmapDrawable) { ((BitmapDrawable)frame).getBitmap().recycle(); } frame.setCallback(null); } loadingAnimation.setCallback(null);
It works fine only for the next or previous. First click on the next animation, moving to the second animation, the second time, if I click on the previous button, I get an exception, for example,
Canvas: trying to use a recycled bitmap android.graphics.Bitmap
please help me.