zoukankan      html  css  js  c++  java
  • BitmapFactory.Options 处理OOM

    问题颇多.parcelfilexxxxx的没接触过 。mark

    ParcelFileDescriptor parcelFD = this.getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor source = parcelFD.getFileDescriptor();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            BitmapFactory.decodeFileDescriptor(source, null, options);
    
            int width = options.outWidth;
            int height = options.outHeight;
    
            int scale = 1;
            if (width > 400f) {
                scale = (int) (width / 400f);
            } else if (height > 640f) {
                scale = (int) (height / 640f);
            }
            if (scale <= 0) {
                scale = 1;
            }
            options.inSampleSize = scale;
    
            options.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFileDescriptor(source, null, options);
    
            return bitmap;
    

    步骤似乎就是这样的。

    第一步:BitmapFactory.Option

    设置 inJustDecodeBounds为true

    第二步:BitmapFactory.decodeFile(path,option)方法

    解码图片路径为一个位图。如果指定的文件名是空的,或者不能解码到一个位图,函数将返回null[空值]。

    获取到outHeight(图片原始高度)和 outWidth(图片的原始宽度)

    第三步:计算缩放比例,也可以不计算,直接给它设定一个值。

    options.inSampleSize = "你的缩放倍数";

    如果是2就是高度和宽度都是原始的一半。

    第四步:设置options.inJustDecodeBounds = false;

    重新读出图片

    bitmap = BitmapFactory.decodeFile(path, options);

  • 相关阅读:
    2008年8月1日21世纪首次日全食奇观
    7.3午饭记
    简单漂亮的导航栏效果
    浮动居中float:center
    图片垂直居中的CSS技巧
    谷歌Chrome浏览器发布
    满江红.中秋寄远
    寄中秋月下独酌
    春江花月夜
    开始锻炼身体
  • 原文地址:https://www.cnblogs.com/lyxin/p/5939717.html
Copyright © 2011-2022 走看看