Android InputStream转Bitmap

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;	     
	}

  

你可能感兴趣的:(Android InputStream转Bitmap)