美文网首页
如何压缩Bitmap避免出现OOM

如何压缩Bitmap避免出现OOM

作者: Stephen__Li | 来源:发表于2017-07-17 17:54 被阅读46次

做android开发最揪心的就是遇到OOM,为什么呢?因为它不是在编译期就报异常,是在运行期才报异常,而且它是积累性的,一次两次运行还不一定能重现,你要找出问题所在也不容易。但是基本可以肯定,在加载大量图片或一张分辨率很高的图片时,很容易出现OOM(内存溢出)。
我碰到的这个OOM是在验证手势密码的页面,因为这个页面会被经常打开,而不是只打开一两次,所以就满足了OOM发生的条件。在这个页面加载的背景图太大,在某些型号的手机上容易出现OOM。


image.png

这段代码是官方的解决方案,可以放心的用:

    public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    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);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

使用的时候把屏幕的宽高传入(因为我的这张图片是填充屏幕的背景图,所以以屏幕宽高设置,其他情况根据imageView的宽高设定):

bgBitmap = ImageUtil.decodeSampledBitmapFromResource(getResources(), R.mipmap.gesture_bg,
                ViewUtil.getScreenWidth(this), ViewUtil.getScreenHeight(this));
iv_bg.setImageBitmap(bgBitmap);

不用时注意销毁bitmap:

@Override
    protected void onDestroy() {
        super.onDestroy();

        if(iv_bg != null){
            iv_bg.setImageBitmap(null);
        }

        if(bgBitmap != null){
            bgBitmap.recycle();
            bgBitmap = null;
        }

        if (mGestureContentView != null) {
            mGestureContentView.destroyBitmap();
        }
    }

所占用的内存前后对比:
改前:


image.png

改后:


image.png

相关文章

  • 如何压缩Bitmap避免出现OOM

    做android开发最揪心的就是遇到OOM,为什么呢?因为它不是在编译期就报异常,是在运行期才报异常,而且它是积累...

  • Bitmap避免OOM

    目录介绍 01.先看一个需求分析案例 02.Bitmap占用内存介绍 03.影响Bitmap占用内存因素 04.图...

  • BitMap压缩以及二次采样

    BitMap压缩以及二次采样 标签: Android 首先我们来了解一下什么是oom? 1.什么是OOM?为什么会...

  • Android获取图片压缩后的Bitmap避免OOM

    版权声明:本文源自简书tianma,转载请务必注明出处: http://www.jianshu.com/p/2f1...

  • 07.Android之多媒体问题

    目录介绍 7.0.0.1 加载bitmap图片的时候需要注意什么?为何bitmap容易造成OOM?如何计算Bitm...

  • Glide 源码分析之一 with

    前言 最近使用到 Glide 加载本地大图,写代码时想看看 Glide 有没有压缩图片避免出现 OOM 问题,随手...

  • Android 内存泄漏和OOM分析(二)

    接下来我们来讨论OOM的问题。 好了,出现了我们的OOM了! 一般造成OOM的最大的原因是Bitmap,Bitma...

  • 图片压缩,二次采样

    图片压缩就是为了避免我们内存溢出,所有要对一系列进行压缩二次采样等 1.什么是OOM?为什么会引起OOM? out...

  • android图片压缩避免OOM

    简单吹下牛:很多app都会要加载图片,但是如果不压缩图片就很容易OOM, 个人看来OOM 出现原因总的来说分为两种...

  • 2019-11-05

    Bitmap Bitmap 细说Bitmap bitmap的六种压缩方式,Android图片压缩 1.先讲讲屏幕密...

网友评论

      本文标题:如何压缩Bitmap避免出现OOM

      本文链接:https://www.haomeiwen.com/subject/kdhqkxtx.html