您的位置:首页 > 移动开发 > Android开发

Android InputStream转Bitmap

2016-03-11 15:32 591 查看
android socket服务端 接收Delphi socket客户端发来的图片,保存到bitmap中,代码如下:

public static Bitmap readInputStreamToBitmap(InputStream ins, int fileSize) {
if (ins == null) {
return null;
}
byte[] b;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[1024];
int size = -1;
int len = 0;// 已经接收长度
size = ins.read(buffer);
while (size != -1) {
len = len + size;//
bos.write(buffer, 0, size);
if (fileSize == len) {// 接收完毕
break;
}
size = ins.read(buffer);
}
b = bos.toByteArray();
bos.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
return null;
}


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