Android InputStream转Bitmap

android socket服务端 接收Delphi socket客户端发来的图片,保存到bitmap中,代码如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;        
}

  

posted @   tc310  阅读(1650)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示