加载大图避免出现OOM

获取程序运行时需要的内存

//获得每个应用程序运行的时的最大内存
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024/1024);
Log.i("info", "Max memory is " + maxMemory + "MB");

需要注意一下注意的几个重要参数及方法

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

1.首先是BitmapFactory.Options类,作用是允许我们定义图片以何种方式如何读到内存。
2.options.inJustDecodeBounds = true
官方文档是这样说明的
这里写图片描述
意思是说如果设置为true那么像BitmapFactory.decodeResource类似这样的方法不会返回一个Bitmap对象而是null值,但是通过设置为true我们可以得到原始图片的宽高值,因而可以去计算inSampleSize的大小,看看Sdk文档是怎么进行描述的

public int inSampleSize
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.

可以将此参数理解为对图片压缩的缩放比,如文档所说的如果inSampleSize == 4,那么将返回图片原始宽高比的1/4,也就是将图片大小压缩了16倍。
(后面几个参数的意思就比较简单了,稍微说一下即可)
3.options.outHeight 图片原始高度
4.options.outWidth 图片原始宽度
5.options.outMimeType 图片MimeType类型
注意outHeight 和outWight依赖于inJustDecodeBounds=true的设置
文档如下:
这里写图片描述

方便以后查看,贴出所有Option的字段
这里写图片描述

亲测2.43mb图片会压缩到273kb
测试源码如下:

public class MainActivity extends Activity {

    private ImageView iv;
    private ImageView iv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获得每个应用程序运行的时的最大内存
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024/1024);
        Log.i("info", "Max memory is " + maxMemory + "MB");
        iv = (ImageView) findViewById(R.id.iv);
        Bitmap bitmap = decodeSampledBitmapFromResource(getResources(),
                R.drawable.icon, 200, 200);
        Log.i("info", "size:"+bitmap.getByteCount()/1024);
        if(bitmap!=null)
            iv.setImageBitmap(bitmap);
    }

    /**
     * 计算压缩比率
     * @param options 
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // 源图片的高度和宽度
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            // 计算出实际宽高和目标宽高的比率
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
            // 一定都会大于等于目标的宽和高。
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }


    /**
     * 得到压缩后的bitmap实例
     * @param res
     * @param resId
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {
        // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        // 调用上面定义的方法计算inSampleSize值
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        // 使用获取到的inSampleSize值再次解析图片
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值