private ByteArrayOutputStream getPicByte(String imgUrl){
try {
ByteArrayOutputStream byteArrayOutputStream = null;
URL url = new URL(imgUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5 * 1000);
InputStream inputStream = connection.getInputStream();
byte [] bytes = new byte[1024];
int index = 0;
byteArrayOutputStream = new ByteArrayOutputStream();
while (-1 != (index = inputStream.read(bytes,0,bytes.length))){
byteArrayOutputStream.write(bytes,0,index);
}
inputStream.close();
return byteArrayOutputStream;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
1、这个下载图片的代码下载只能下载一半图片显示不完整,不知道什么问题。
2、怎么让怎么函数直接返回Image对象呢,对IO流不是很懂。