首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android 请求网络图片的有关问题

2013-11-14 
Android 请求网络图片的问题本帖最后由 xl_0715 于 2013-11-04 18:09:38 编辑代码:public static Bitmap g

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;
}

代码执行没效果,我开的tomcat,在手机浏览器里可以正常访问到图片,
但是用上面的代码,调试的时候,报异常 java.net.ProtocolException: Connection already established
希望帮忙看看 android
[解决办法]
HttpURLConnection??conn?=?(HttpURLConnection)url.openConnection();
这个conn.connect()一下要加。

热点排行