android oom异常是什么,Android 图片过大导致的OOM异常解决方案

原文链接:http://www.ytsyt.cn/post/7.html

先转载收藏!

手机的拍照像素越来越高,拍出来的照片效果也会越来越好,但是像素高了照片的容量也就变大了,如果不能处理好bitmap的话很容易导致OOM异常的。以下提供3种解决OOM异常的方案:

一、bitmap不用就就回收掉

Java protected void onDestroy() {

super.onDestroy();

if(bmp1 != null){

bmp1.recycle();

bmp1 = null;

}

if(bmp2 != null){

bmp2.recycle();

bmp2 = null;

}

}

二、先算出该bitmap的大小,然后通过调节采样率的方式来规避,也就是等比例压缩

Java BitmapFactory.Options opts = new BitmapFactory.Options();

opts.inJustDecodeBounds = true;

BitmapFactory.decodeFile(imageFile, opts);

opts.inSampleSize = computeSampleSize(opts, minSideLength, maxNumOfPixels);

opts.inJustDecodeBounds = false;

try {

return BitmapFactory.decodeFile(imageFile, opts);

} catch (OutOfMemoryError err) { }

return null;

三、在进行文件传输时,最好采用压缩的方式变成byte[]再传输

Java public static byte[] bitmap2Bytes(Bitmap bm) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 90, baos);

return baos.toByteArray();

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值