提供几个处理图片内存溢出的方法

/**
  * 图片按比例大小压缩方法(根据Bitmap图片压缩):
  * @param image
  * @return Bitmap
  */
 public Bitmap compressBmpByBm(Bitmap bitmap) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  if (baos.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
   baos.reset();// 重置baos即清空baos
   bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 这里压缩50%,把压缩后的数据存放到baos中
  }
  ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
  BitmapFactory.Options opts = new BitmapFactory.Options();
  // 开始读入图片,此时把options.inJustDecodeBounds 设回true了
  opts.inJustDecodeBounds = true;
  BitmapFactory.decodeStream(isBm, null, opts);
  opts.inJustDecodeBounds = false;
  opts.inSampleSize = getSampleSize(opts);// 设置缩放比例
  // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
  isBm = new ByteArrayInputStream(baos.toByteArray());
  bitmap = BitmapFactory.decodeStream(isBm, null, opts);
  return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
 }

 

// 获取缩放比例值
 private int getSampleSize(BitmapFactory.Options opts) {// 获取手机像素
  float pW = getMetrics().widthPixels;
  float pH = getMetrics().heightPixels;
  int be = 1;
  if (opts.outWidth > opts.outHeight && opts.outWidth > pW) {
          be = (int) (opts.outWidth / pW);
  } else if (opts.outWidth < opts.outHeight && opts.outHeight > pH) {
          be = (int) (opts.outHeight / pH);
  }
  if (be <= 0) {
          be = 1;
  }
  return be;
 }

 

  public Bitmap compressBmpByResid(int resid) {
  Bitmap bitmap = null;
  try {
   BitmapFactory.Options opts = new BitmapFactory.Options();
   // Uri uri =
   // Uri.parse("android.resource://com.test.oom/drawable-hdpi/hb4.JPG");
   opts.inJustDecodeBounds = true;
   
   // 节约内存
   opts.inPreferredConfig = Bitmap.Config.ARGB_4444; // 默认是Bitmap.Config.ARGB_8888
   /* 下面两个字段需要组合使用 */
   opts.inPurgeable = true;
   opts.inInputShareable = true;

   BitmapFactory.decodeResource(getResources(), resid, opts);
   opts.inSampleSize = getSampleSize(opts);
   opts.inJustDecodeBounds = false; // 根据已经得到的缩放比例得到自己想要的图片缩放图
   bitmap = BitmapFactory.decodeResource(getResources(), resid, opts);
//   bitmap = BitmapFactory.decodeStream(getResources().getAssets().open(fileUrl), null, opts);
   compressImage(bitmap);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return bitmap;
 }

 

/**
  * 图片按比例大小压缩方法(根据路径获取图片并压缩):
  * 路径只支持SDCard的路径像Asset或res不支持
  */
 public Bitmap compressBmpByPath(String srcPath) {
  BitmapFactory.Options opts = new BitmapFactory.Options();
  //开始读入图片,此时把options.inJustDecodeBounds 设回true了
  /**
   * 设为true那么将不返回实际的bitmap对象,不给其分配内存空间但是可以得到一些解码边界信息即图片大小等信息。
   * 因此我们可以通过设置inJustDecodeBounds为true,获取到outHeight(图片原始高度)和
   * outWidth(图片的原始宽度
   * ),然后计算一个inSampleSize(缩放值),就可以取图片了,这里要注意的是,inSampleSize
   * 可能等于0,必须做判断。也就是说先将Options的属性inJustDecodeBounds设为true
   * ,先获取图片的基本大小信息数据
   * (信息没有保存在bitmap里面,而是保存在options里面),通过options.outHeight和 options.
   * outWidth获取的大小信息以及自己想要到得图片大小计算出来缩放比例inSampleSize
   */
  opts.inJustDecodeBounds = true;
  opts.inPreferredConfig = Bitmap.Config.ARGB_4444; // 默认是Bitmap.Config.ARGB_8888 
  Bitmap bitmap = BitmapFactory.decodeFile(srcPath,opts);//此时返回bm为空
  opts.inJustDecodeBounds = false;
  opts.inSampleSize = getSampleSize(opts);//设置缩放比例
  //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
  bitmap = BitmapFactory.decodeFile(srcPath, opts);
  return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值