国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 圖片處理避免出現(xiàn)oom的方法詳解

2019-12-12 02:03:32
字體:
供稿:網(wǎng)友

1. 通過設(shè)置采樣率壓縮

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

uri圖片壓縮 decodeStream

  public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, 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), UtilUnitConversion.dip2px(MyApplication.mContext, reqHeight));    options.inJustDecodeBounds = false;    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, 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(), null, opts);            opts.inDither = false;            opts.inPurgeable = true;            opts.inInputShareable = true;            opts.inTempStorage = new byte[32 * 1024];            opts.inSampleSize = BitmapUtils.calculateInSampleSize(opts,                UtilUnitConversion.dip2px(MyApplication.mContext, width), UtilUnitConversion.dip2px(MyApplication.mContext, height));            opts.inJustDecodeBounds = false;            bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(),                null, opts);          } catch (IOException e) {            e.printStackTrace();          } finally {            if (null != fs) {              try {                fs.close();              } catch (IOException e) {                e.printStackTrace();              }            }          }        }      }    }    return bitmap;  }

根據(jù)顯示的圖片大小進行SampleSize的計算

public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, 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;  }

調(diào)用方式:

復制代碼 代碼如下:

mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myImage, 100, 100))

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

2. 質(zhì)量壓縮:指定圖片縮小到xkb以下

  // 壓縮到100kb以下  int maxSize = 100 * 1024;  public static Bitmap getBitmapByte(Bitmap oriBitmap, int maxSize) {    ByteArrayOutputStream out = new ByteArrayOutputStream();    oriBitmap.compress(Bitmap.CompressFormat.JPEG, 100, 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...

也就是說,如果我們把它設(shè)為true,那么BitmapFactory.decodeFile(String path, Options opt)并不會真的返回一個Bitmap給你,它僅僅會把它的寬,高取回來給你,這樣就不會占用太多的內(nèi)存,也就不會那么頻繁的發(fā)生OOM了。

  /**   * 根據(jù)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, resId, options);    return options;  }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安塞县| 抚顺市| 西安市| 德兴市| 肇庆市| 栾城县| 泊头市| 河北区| 扶绥县| 海安县| 延川县| 西乡县| 紫云| 城步| 和平区| 平山县| 济源市| 寻乌县| 五常市| 通山县| 大姚县| 江北区| 洛隆县| 陵水| 兴义市| 济阳县| 虹口区| 丰原市| 建水县| 龙里县| 桂平市| 衡南县| 定结县| 睢宁县| 如东县| 西峡县| 南安市| 日照市| 平遥县| 马关县| 云安县|