xiejunna 2016-06-08 08:25 采纳率: 0%
浏览 1173

android中图片压缩过程中,获取bitmap时,OOM,怎么办?

图片在压缩过程中,OOM,bm = BitmapFactory.decodeFile(file.getPath(), opt); 是这一行报错,大神们帮忙想下办法吧,谢谢了!

BitmapFactory.Options opt = new BitmapFactory.Options();
// 这个isjustdecodebounds很重要
opt.inJustDecodeBounds = true;
Bitmap bm = BitmapFactory.decodeFile(file.getPath(), opt);

    // 获取到这个图片的原始宽度和高度
    int picWidth = opt.outWidth;
    int picHeight = opt.outHeight;
    // 获取屏的宽度和高度
    WindowManager windowManager = context.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();

    // isSampleSize是表示对图片的缩放程度,比如值为2图片的宽度和高度都变为以前的1/2
    //opt.inSampleSize = 1;
    int a=0;
    // 根据屏的大小和图片大小计算出缩放比例
    if (picWidth > picHeight) {
        if (picWidth > screenWidth)
            //opt.inSampleSize = picWidth / screenWidth;
        a= picWidth / screenWidth;
    } else {
        if (picHeight > screenHeight)

            // opt.inSampleSize = picHeight / screenHeight;
        a = picHeight / screenHeight;

    }

    if(a<=0){a=3;}

    opt.inSampleSize = a;


    // 这次再真正地生成一个有像素的,经过缩放了的bitmap
    opt.inJustDecodeBounds = false;
    try {
        bm = BitmapFactory.decodeFile(file.getPath(), opt); 
        //压缩后的宽和高
        int w = bm.getWidth();
        int h = bm.getHeight();
        return bm;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
  • 写回答

1条回答 默认 最新

  • lbcab 2016-06-08 08:49
    关注

    opt.inPreferredConfig = Bitmap.Config.RGB_565;
    加上这句试试.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向