根据URL获取图片
private Bitmap getImageFromNet(String imageUrl) {
HttpURLConnection conn=null;
try {
//创建一个URL
URL url=new URL(imageUrl);
//获取一个连接
conn = (HttpURLConnection) url.openConnection();
//设置请求方式
conn.setRequestMethod("GET");
//设置连接超时
conn.setConnectTimeout(10000);
//设置读取超时
conn.setReadTimeout(10000);
//开始连接
conn.connect();
//获取连接响应码
int code = conn.getResponseCode();
if(code==200){
InputStream is=conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}else{
Log.i("响应码", String.valueOf(code));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
conn.disconnect();
}
}
return null;
}