0

如何解决android中的OOM问题。我已经尝试了几乎所有的事情,比如缩放位图、BitmapOption 中的 inPurgeable、释放所有资源等,但仍然遇到 OOM 问题。这基本上是在相机拍摄的图像或任何大于 1.5 mb 的图像中。我的应用程序中还有 15-20 mb 大小的图像。

4

2 回答 2

0

你试过Bitmap.recycle();吗?

它曾经解决了我的内存不足问题。

于 2013-12-11T11:58:43.197 回答
0

这是我正在做的避免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 等文件夹......我只是使用带有大位图的可绘制对象,并进行缩放。这样你就可以在最终的 apk 文件中节省一些 mb

安卓培训代码在这里获取更多信息http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

呸!希望这会有所帮助,我花了一天的时间试图弄清楚这一点并阅读了这个论坛中的所有问题和答案。这只是背景图像的示例,但我的游戏中有 20 多张图像都以这种方式加载,但输出尺寸较小,而且运行非常流畅。

于 2013-06-16T13:56:42.733 回答