你的位置:首页 > 操作系统

[操作系统]android bitmap和数据流的互转


Bitmap aa = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
InputStream input = datastream(aa);//把bitmap转换为流
Bitmap bitmap = transformation(input);//把流转换为bitmap

public InputStream datastream (Bitmap bm){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
ByteArrayInputStream is = new ByteArrayInputStream(stream.toByteArray());
return is;
}

public Bitmap transformation(InputStream input){
if(input != null){
int len =-1;
byte[] temp = new byte[4 * 1024];
ByteArrayBuffer buffer = new ByteArrayBuffer(2 * 1024 * 1024);
try {
while ((len = input.read(temp)) != -1) {
buffer.append(temp,0,len);
}
Bitmap bmp = BitmapFactory.decodeByteArray(buffer.buffer(), 0, buffer.length());//把流转换为bitmap
} catch (IOException e) {
e.printStackTrace();
}
}
return bmp;
}