java 图片解析,血的教训 BitmapFactory.decodeByteArray() 是个邪恶的方法

    BitmapFactory.decodeByteArray()  是个邪恶的方法  , 在android 上 很容易 OOM。


     正解如下:


/**
* 将从Message中获取的,表示图片的字符串解析为Bitmap对象

* @param picStrInMsg
* @return
*/
public static Bitmap decodeImg(String picStrInMsg) {
Bitmap bitmap = null;

byte[] imgByte = null;
InputStream input = null;
try{
imgByte = Base64.decode(picStrInMsg, Base64.DEFAULT);
       BitmapFactory.Options options=new BitmapFactory.Options();
       options.inSampleSize = 8;
       input = new ByteArrayInputStream(imgByte);
       SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
       bitmap = (Bitmap)softRef.get();;
}catch(Exception e){
e.printStackTrace();
}finally{
if(imgByte!=null){
imgByte = null;
}

if(input!=null){
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

//
// byte[] imgByte = Base64.decode(picStrInMsg, Base64.DEFAULT);
//
// try {
// bitmap = BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
// imgByte = null;
// } catch (OutOfMemoryError e) {
// e.printStackTrace();
// try {
// bitmap = BitmapFactory.decodeByteArray(imgByte, 0,
// imgByte.length);
// } catch (OutOfMemoryError e1) {
// e.printStackTrace();
// } catch (Exception e1) {
// e.printStackTrace();
// }
// } catch (Exception e) {
// e.printStackTrace();
// }


return bitmap;
}

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值