如何解决的OutOfMemoryError在android系统? [英] how to solve OutOfMemoryError in android?
问题描述
我有ppared节数可绘制animations.when应用$ P $推出的第一个动画将start.i有两个按钮(Next和previous)具有相同的activity.when我点击下一步按钮我有一样例外
I have prepared no.of drawable animations.when application launch first animation will be start.i have two buttons(next and previous) with same activity.when i click on next button i got exception like,
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
我的code是,
My code is,
有关从绘制获得绘制动画
For getting drawable animations from 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();
}
});
}
我的下一个按钮code是,
my next button code is,
case R.id.btnNext_:
unbindDrawables(findViewById(R.id.imgLetter));
mLetterNum=mLetterNum+1;
getAnimationForLetters(mLetterNum);
我undind绘制方法code是,
my undind drawable method code is,
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();
}
}
终于,我得到异常 java.lang.OutOfMemoryError:位图的大小超过VM预算
这正显示出这里的异常 mImgLetter.setBackgroundResource(渣油);
请帮我。
我已经加入以下code清除,
I have added following code to clear,
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);
这是工作的罚款,仅次于或下一个动画转移到秒的动画,第二次previous.First时候按一下,如果我在previous按钮点击我有一样例外,
It is working fine for only next or previous.First time click on next animation move to second animation,second time if i click on previous button i got exception like,
Canvas: trying to use a recycled bitmap android.graphics.Bitmap
请帮我。
推荐答案
准确 - 系统知道何时收集垃圾,因此调用不中至少帮助
exactly - the system knows when to collect garbage, so calling that doesn't help in the least.
您必须真的管理你的应用程序的内存。 290x330可能似乎不是很多,但如果你使用的全RGBA这是每像素4个字节,图像变成380K。如果你有几个这样你会耗尽内存。大多数Android设备不像电脑 - 他们已经相当有限的内存。有的只有16M或24M运行的一切,包括操作系统和任何其他应用程序的用户可能会同时运行。
You have to really really manage your apps memory. 290x330 may not seem like much, but if you're using full RGBA that's 4 bytes per pixel and your image turns into 380K. If you have several of these you're going to run out of memory. Most Android devices are not like PCs - they have rather limited memory. Some have only 16M or 24M to run everything, including the OS and any other apps the user might be running concurrently.
您可以尝试包装你的日常用来加载的try / catch资源
You might try wrapping your routine to load resources with try/catch
While( ! success ) {
try {
// load resources
} catch (OutOfMemoryError E) {
// do something to use less memory and try again
}
}
这篇关于如何解决的OutOfMemoryError在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!