如何解决android中的OOM问题。我尝试了几乎所有的东西,像缩放位图,BitmapOption中的inPurgeable,释放所有资源等等,但仍然有OOM问题。这基本上是由相机拍摄的图像或任何图像,即大于1.5mb。我也有15-20mb大小的图片在我的应用程序。
发布于 2013-06-16 21:56:43
这就是我为了避免OOM错误所做的事情,使用的是android培训中的一些代码。这是在我的"ScaledFactor“课上
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inPurgeable = true; // I aded this to the android training code, without this I still have OOM errors. so try using inPurgeable = true
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}在我的活动中,我使用
background = ScaledFactor.decodeSampledBitmapFromResource(getResources(), R.drawable.menu, screenheight, screenwidth); // screeenhieght is the output height , screenwidth is the output width然后在on destroy方法中,或者在调用其他意图之后,我使用background.recycle();
我没有使用hdpi,ldpi和so文件夹...我只是使用带有较大位图的可绘制图形,并进行缩放。这样可以在最终的apk文件中节省一些mb。
有关更多信息,请参阅此处的安卓培训代码http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap
来吧!希望这对我有所帮助,我花了一天的时间试图弄清楚这一点,并阅读了这个论坛上的所有问题和答案。这只是背景图像的例子,但我的游戏中有20多个图像都是以这种方式加载的,但输出尺寸较小,而且非常流畅。
发布于 2013-12-11 19:58:43
你试过Bitmap.recycle();吗?
它曾经解决了我的内存不足问题。
https://stackoverflow.com/questions/13651628
复制相似问题