关于图片的oom

来源:互联网 发布:让淘宝卖家大哭的评论 编辑:程序博客网 时间:2024/04/27 18:14

关于图片的oom

oom(out  of  memory),顾名思义,就是内存溢出,一般出现在加载大图片时候碰到,最近做camera这块,也出现了oom的问题,这里推荐个比较好的压缩方法

 public static  Bitmap revitionImageSize(Bitmap image)throws IOException   {   ByteArrayOutputStream baos = new ByteArrayOutputStream();            image.compress(Bitmap.CompressFormat.JPEG, 100, baos);   ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());   BitmapFactory.Options newOpts = new BitmapFactory.Options(); <span class="comment"><span style="color:#008200;">                         // 设置为true,不为bitmap分配内存空间,只记录一些该图片的信息(例如图片大小)</span></span> newOpts.inJustDecodeBounds = true;   BitmapFactory.decodeStream(isBm, null, newOpts);   isBm.close();   int i = 1;          Bitmap bitmap = null;          while (true) {              // 这一步是根据要设置的大小,使宽和高都能满足           int w =newOpts.outWidth;        int h = newOpts.outHeight;            if ((newOpts.outWidth >> i <= image.getWidth())                      && (newOpts.outHeight >> i <= image.getHeight())) {                  // 重新取得流,注意:这里一定要再次加载,不能二次使用之前的流!               isBm = new ByteArrayInputStream(baos.toByteArray());                 // 这个参数表示 新生成的图片为原始图片的几分之一。                   int x =(int) Math.pow(2.0D, i);                newOpts.inSampleSize = (int) Math.pow(2.0D, i);                // 这里之前设置为了true,所以要改为false,否则就创建不出图片                   newOpts.inJustDecodeBounds = false;                  bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);                  break;              }              i += 1;          }          return bitmap;   }
我这里是压缩了一倍,具体要压缩多少,自己可以设置,inSampleSize就是你最后要压缩倍数的值。项目中用到了,在此记录一下!

0 0
原创粉丝点击