通過URL讀取圖片

通過inputStream讀取圖片方法

public static byte[] readStream(InputStream inStream) throws Exception {  
	ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
	byte[] buffer = new byte[1024];  
	int len = -1;  
	while ((len = inStream.read(buffer)) != -1) {  
             //write(byte[] b, int off, int len)
             //將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此 byte 數組輸出流。
             outSteam.write(buffer, 0, len);
	}  
	outSteam.close();  
	inStream.close();  
	return outSteam.toByteArray();  
}

通過圖片url可以獲得inputStream

URL url = new URL(image_url);
  InputStream is = url.openStream(); 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章