图片下载一般是分为两个步骤:
1.(在新线程中)将输入流转换为字节数组下载,下载完之后把该close的都close掉,节省资源。
URL url =new URL(“此处传入下载地址”);
HttpURLConnection connection =(HttpURLConnection)url.openconnection();
private byte[] =picByte;
private String =
URL url =new URL("此处传入下载地址");
HttpURLConnection connection =(HttpURLConnection)url.openconnection();
InputStream is =connection.getInputStream();
ByteArrayOutputStream bao =new ByteArrayOutputStream();
int length =-1;
byte [] bytes =new byte[1024];
while((length =is.read(bytes)) !=-1){
bao.write(bytes,0,length);
}
is.close();
bao.close();
2.将字节数组解析成Bitmap,然后显示出来。
private byte[] picByte;
private ImageView loadImg;
if(picByte !=null){
Bitmap bitmap =BitmapFactory.decodeByteArray(picByte,0,picByte.length);
loadImg.setImageBitmap(bitmap);
}