您的位置:首页 > 其它

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

2014-11-03 16:55 447 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: