当前位置: 首页 > 编程笔记 >

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

叶嘉颖
2023-03-14
本文向大家介绍Android 图片处理避免出现oom的方法详解,包括了Android 图片处理避免出现oom的html" target="_blank">方法详解的使用技巧和注意事项,需要的朋友参考一下

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, 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;
  }

根据显示的图片大小进行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;
  }

调用方式:


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. 质量压缩:指定图片缩小到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...

也就是说,如果我们把它设为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, resId, options);
    return options;
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍Android利用软引用和弱引用避免OOM的方法,包括了Android利用软引用和弱引用避免OOM的方法的使用技巧和注意事项,需要的朋友参考一下 想必很多朋友对OOM(OutOfMemory)这个错误不会陌生,而当遇到这种错误如何有效地解决这个问题呢?今天我们就来说一下如何利用软引用和弱引用来有效地解决程序中出现的OOM问题. 一.了解 强引用、软引用、弱引用、虚引用的概念   在J

  • 本文向大家介绍Spark处理数据排序问题如何避免OOM,包括了Spark处理数据排序问题如何避免OOM的使用技巧和注意事项,需要的朋友参考一下 错误思想 举个列子,当我们想要比较 一个 类型为 RDD[(Long, (String, Int))] 的RDD,让它先按Long分组,然后按int的值进行倒序排序,最容易想到的思维就是先分组,然后把Iterable 转换为 list,然后sortby,但

  • 本文向大家介绍Android高效加载大图、多图解决方案 有效避免程序OOM,包括了Android高效加载大图、多图解决方案 有效避免程序OOM的使用技巧和注意事项,需要的朋友参考一下 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工,英文好的朋友也可以直接去读原文。 http://developer.android.com/training/displaying-bitmaps

  • 本文向大家介绍springMVC图片上传的处理方式详解,包括了springMVC图片上传的处理方式详解的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了springMVC图片上传的处理方式,供大家参考,具体内容如下 首先需要依赖的jar包: 页面: 大家如果觉得默认的上传文件的按钮不好看,可以引入下面的css样式: 前端脚本: 后端接收: 以上就是本文的全部内容,希望对大家的学习有所帮

  • 本文向大家介绍Android中3种图片压缩处理方法,包括了Android中3种图片压缩处理方法的使用技巧和注意事项,需要的朋友参考一下 Android中图片的存在形式: 1:文件形式:二进制形式存在与硬盘中。 2:流的形式:二进制形式存在与内存中。 3:Bitmap的形式 三种形式的区别: 文件形式和流的形式:对图片体积大小并没有影响。也就是说,如果你手机SD卡上的图片通过流的形式读到内存中,在内

  • 本文向大家介绍Android编程之图片颜色处理方法,包括了Android编程之图片颜色处理方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程之图片颜色处理方法。分享给大家供大家参考,具体如下: 你想做到跟美图秀秀一样可以处理自己的照片,美化自己的照片吗?其实你也可以自己做一个这样的软件,废话不多说了,直接上图,上代码了! 效果图如下: 没处理前: 处理之后: MainA