获取一张网络图片(安卓)

时间:2022-04-20 22:37:52
<pre class="java" name="code">	public InputStream getInputStream(String url) throws IOException{
		URL _url = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) _url.openConnection();//基于HTTP协议连接对象
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		return conn.getInputStream();
	}
	public byte[] getBytesFromInputStream(InputStream inStream) throws IOException{
		ByteArrayOutputStream out=new ByteArrayOutputStream();
		byte[] buffer=new byte[1024];
		int length=0;
		while((length=inStream.read(buffer))!=-1){
			out.write(buffer);
		}
		inStream.close();
		return out.toByteArray();
	}