最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年09月11日 14:16:06   作者:燊在錦官城_  
本篇文章主要介紹了Android 圖片處理避免出現(xiàn)oom的方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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ù)顯示的圖片大小進(jìn)行SampleSize的計(jì)算

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)用方式:

復(fù)制代碼 代碼如下:

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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android仿微信語音聊天界面設(shè)計(jì)

    Android仿微信語音聊天界面設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了Android仿微信語音聊天界面設(shè)計(jì)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 小心!Listview結(jié)合EditText使用實(shí)例中遇到的那些坑

    小心!Listview結(jié)合EditText使用實(shí)例中遇到的那些坑

    小心!Listview結(jié)合EditText使用實(shí)例中遇到的那些坑,解決EditText焦點(diǎn)丟失、保存數(shù)據(jù)以及滾動沖突的問題,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android虛擬機(jī)Dalvik和ART科普

    Android虛擬機(jī)Dalvik和ART科普

    這篇文章主要為大家介紹了Android虛擬機(jī)Dalvik和ART科普詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android自定義view實(shí)現(xiàn)滑動解鎖效果

    Android自定義view實(shí)現(xiàn)滑動解鎖效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)滑動解鎖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android標(biāo)題欄中添加返回按鈕功能

    Android標(biāo)題欄中添加返回按鈕功能

    標(biāo)題欄中的返回按鈕在實(shí)際使用中用的比較多,今天就來講講我在項(xiàng)目開發(fā)中的使用經(jīng)歷,需要的朋友參考下吧
    2017-04-04
  • Android實(shí)現(xiàn)雙向滑動特效的實(shí)例代碼

    Android實(shí)現(xiàn)雙向滑動特效的實(shí)例代碼

    這篇文章主要介紹了Android實(shí)現(xiàn)雙向滑動特效的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助,一起跟隨小編過來看看吧
    2018-05-05
  • Android光線傳感器使用方法詳解

    Android光線傳感器使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android光線傳感器的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android編程中黑名單的實(shí)現(xiàn)方法

    Android編程中黑名單的實(shí)現(xiàn)方法

    這篇文章主要介紹了Android編程中黑名單的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android通過對比通信錄及自動掛斷電話等技巧實(shí)現(xiàn)黑名單功能的功能,需要的朋友可以參考下
    2016-02-02
  • Android中標(biāo)簽容器控件的實(shí)例詳解

    Android中標(biāo)簽容器控件的實(shí)例詳解

    在Android開發(fā)過程中,常常會遇到這樣的場景:我們展示一種物品或者為某一事物添加一些標(biāo)簽。比如說,我們買一件衣服,可以有以下幾種標(biāo)簽:杰克瓊斯,男士,運(yùn)動等等。本文將實(shí)例介紹Android中標(biāo)簽容器控件的實(shí)現(xiàn)過程。
    2016-07-07
  • android fm單體聲和立體聲的切換示例代碼

    android fm單體聲和立體聲的切換示例代碼

    切換是需要在一定的條件下滿足才會進(jìn)行切換,切換的條件和電臺的信號強(qiáng)度RSSI、信號穩(wěn)定性CQI等等都有關(guān)系
    2013-06-06

最新評論

南投市| 虹口区| 民丰县| 安化县| 成都市| 怀仁县| 高台县| 江门市| 凤山县| 曲沃县| 双桥区| 历史| 福安市| 佛冈县| 黄大仙区| 南丹县| 林芝县| 沽源县| 林西县| 新营市| 四子王旗| 固镇县| 阳西县| 凤山县| 河池市| 南丰县| 灵川县| 星子县| 新沂市| 元谋县| 郁南县| 五寨县| 高雄市| 建平县| 车致| 吉首市| 太保市| 黑龙江省| 庄浪县| 门头沟区| 玉田县|