Androiod字符串转Bitmap导致的内存溢出
BitmapFactory.decodeByteArray
byte[] bitmapArray = Base64.decode(string, Base64.DEFAULT);
//BitmapFactory.decodeByteArray在数据较多时,可能会导致内存溢出
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
使用以下方法
byte[] bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = byteToBitmap(bitmapArray);
----------------------------------------------------
private Bitmap byteToBitmap(byte[] imgByte) {
InputStream input = null;
Bitmap bitmap = null;
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();
if (imgByte != null) {
imgByte = null;
}
try {
if (input != null) {
input.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
Base64字符串图片的问题
在接口中返回包含有Base64字符串图片的列表数据时,会导致解析缓慢,图片也因为不能缓存,进一步导致图片显示缓慢。应避免使用Base64字符串的方式返回大量图片。