Android 图片处理避免出现oom的方法详解

Android 图片处理避免出现oom的方法详解,第1张

概述1.通过设置采样率压缩res资源图片压缩decodeResourcepublicBitmapdecodeSampledBitmapFromResource(Resourcesres,intresId,intreqWidth,intreqHeight){

1. 通过设置采样率压缩

res资源图片压缩 decodeResource

  public 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,options);  }

uri图片压缩 decodeStream

  public Bitmap decodeSampledBitmapFromUri(Uri uri,int reqHeight) {    Bitmap bitmap = null;    try {    BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;    BitmapFactory.decodeStream(getContentResolver().openinputStream(uri),null,options);    options.inSampleSize = BitmapUtils.calculateInSampleSize(options,UtilUnitConversion.dip2px(MyApplication.mContext,reqWIDth),reqHeight));    options.inJustDecodeBounds = false;    bitmap = BitmapFactory.decodeStream(getContentResolver().openinputStream(uri),options);    } catch (Exception e) {      e.printstacktrace();    }    return bitmap;  }

本地file url图片压缩

  public static Bitmap getloadlBitmap(String load_url,int wIDth,int height) {    Bitmap bitmap = null;    if (!UtilText.isEmpty(load_url)) {      file file = new file(load_url);      if (file.exists()) {        fileinputStream fs = null;        try {          fs = new fileinputStream(file);        } catch (fileNotFoundException e) {          e.printstacktrace();        }        if (null != fs) {          try {            BitmapFactory.Options opts = new BitmapFactory.Options();            opts.inJustDecodeBounds = true;            BitmapFactory.decodefileDescriptor(fs.getFD(),opts);            opts.inDither = false;            opts.inPurgeable = true;            opts.ininputShareable = true;            opts.inTempStorage = new byte[32 * 1024];            opts.inSampleSize = BitmapUtils.calculateInSampleSize(opts,wIDth),height));            opts.inJustDecodeBounds = false;            bitmap = BitmapFactory.decodefileDescriptor(fs.getFD(),opts);          } catch (IOException e) {            e.printstacktrace();          } finally {            if (null != fs) {              try {                fs.close();              } catch (IOException e) {                e.printstacktrace();              }            }          }        }      }    }    return bitmap;  }

根据显示的图片大小进行SampleSize的计算

public int calculateInSampleSize(BitmapFactory.Options options,int reqHeight) {    if (reqWIDth == 0 || reqHeight == 0) {      return 1;    }    // 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;  }

调用方式:

复制代码 代码如下:
mImageVIEw.setimageBitmap(decodeSampledBitmapFromresource(getResources(),R.ID.myImage,100,100))

Bitmap bitmap = decodeSampledBitmapFromUri(cropfileUri);
UtilBitmap.setimageBitmap(mContext,mImage,UtilBitmap.getloadlBitmap(url,100),R.drawable.ic_login_head,true);

2. 质量压缩:指定图片缩小到xkb以下

  // 压缩到100kb以下  int maxSize = 100 * 1024;  public static Bitmap getBitmapByte(Bitmap oriBitmap,int maxSize) {    ByteArrayOutputStream out = new ByteArrayOutputStream();    oriBitmap.compress(Bitmap.CompressFormat.JPEG,out);    byte[] fileBytes = out.toByteArray();    int be = (maxSize * 100) / fileBytes.length;    if (be > 100) {      be = 100;    }    out.reset();    oriBitmap.compress(Bitmap.CompressFormat.JPEG,be,out);    return oriBitmap;  }

3. 单纯获取图片宽高避免oom的办法

itmapFactory.Options这个类,有一个字段叫做 inJustDecodeBounds 。SDK中对这个成员的说明是这样的:
If set to true,the decoder will return null (no bitmap),but the out...

也就是说,如果我们把它设为true,那么BitmapFactory.decodefile(String path,Options opt)并不会真的返回一个Bitmap给你,它仅仅会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。

  /**   * 根据res获取Options,来获取宽高outWIDth和options.outHeight   * @param res   * @param resID   * @return   */  public static BitmapFactory.Options decodeOptionsFromresource(Resources res,int resID) {    // First decode with inJustDecodeBounds=true to check dimensions    final BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;    BitmapFactory.decodeResource(res,options);    return options;  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android 图片处理避免出现oom的方法详解全部内容,希望文章能够帮你解决Android 图片处理避免出现oom的方法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存