String url="http;//--------";
ImageView image=(ImageView)findViewById(R.id.image);
image.setImageBitmap(get_icon(url));
//获取图片
public Bitmap get_icon(String url){
URL imageUrl=null;
Bitmap bitmap=null;
//new URL 对象将网址传入
try {
imageUrl=new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//取得联机
HttpURLConnection conn;
try {
conn = (HttpURLConnection)imageUrl.openConnection();
conn.connect();
//取得回传的InputStream
InputStream is=conn.getInputStream();
//将InputStream 变为Bitmap
bitmap=BitmapFactory.decodeStream(is);
//关闭InputStream
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
扩展学习:
如果我们想要得知每一次HttpURLConnection 连接的状态,可以利用 HttpURLConnection.getResponseCode取得目前网络连接的服务器响应代码,或以HttpURLConnection.getResponseMessage取得返回的信息。常出现的代码与信息说明
ResponseCode | ResponseMessage | 说明 |
200 | OK | 成功 |
401 | Unauthorized | 未授权 |
500 | Internal Server Error | 服务器内部错误 |
404 | Not Found | 找不到该页 |