Android 请求网络图片的问题
本帖最后由 xl_0715 于 2013-11-04 18:09:38 编辑 代码:
public static Bitmap getImage(String address) throws Exception {
Bitmap imgmap = null;
InputStream is = null;
// 模拟浏览器访问图片
// 创建URL实例
URL url = new URL(address);
// 创建Http连接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
try {
// 指定请求的方法(get、post)
conn.setRequestMethod("GET");
// 设置过期时间
conn.setConnectTimeout(5000);
// 获取服务器访问的流
is = conn.getInputStream();
byte[] imgbytes = StreamTool.getBytes(is);
imgmap = BitmapFactory.decodeByteArray(imgbytes, 0, imgbytes.length);
} finally {
if (is != null) {
is.close();
}
if (conn != null) {
conn.disconnect();
}
}
return imgmap;
}