首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OOM问题android

OOM问题android
EN

Stack Overflow用户
提问于 2012-12-01 03:15:10
回答 2查看 285关注 0票数 0

如何解决android中的OOM问题。我尝试了几乎所有的东西,像缩放位图,BitmapOption中的inPurgeable,释放所有资源等等,但仍然有OOM问题。这基本上是由相机拍摄的图像或任何图像,即大于1.5mb。我也有15-20mb大小的图片在我的应用程序。

EN

回答 2

Stack Overflow用户

发布于 2013-06-16 21:56:43

这就是我为了避免OOM错误所做的事情,使用的是android培训中的一些代码。这是在我的"ScaledFactor“课上

代码语言:javascript
复制
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);
}

在我的活动中,我使用

代码语言:javascript
复制
            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多个图像都是以这种方式加载的,但输出尺寸较小,而且非常流畅。

票数 0
EN

Stack Overflow用户

发布于 2013-12-11 19:58:43

你试过Bitmap.recycle();吗?

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13651628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档