Android为TV端助力 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;
}

posted @ 2016-05-27 16:10  水柠檬QAQ  阅读(1119)  评论(0编辑  收藏  举报