血的教训 BitmapFactory.decodeByteArray() 是个邪恶的方法

http://blog.csdn.net/cnsxhza985/article/details/19625413

 

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

posted @ 2014-11-03 17:20  口我  阅读(3749)  评论(0编辑  收藏  举报