您的位置:首页 > 理论基础 > 计算机网络

HttpURLConnection从网上下载图片

2016-07-21 17:44 453 查看
public class HttpThread extends Thread {

private final String url;

private Handler handler;

public WebView webView;

public ImageView imageView;

public HttpThread(String url, WebView webView, Handler handler) {
this.url = url;
this.webView = webView;
this.handler = handler;
}

public HttpThread(String url,ImageView imageView,Handler handler)
{
this.imageView=imageView;
this.url =url;
this.handler=handler;
}
@Override
public void run() {
URL httpUrl = null;
try {
httpUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream in=conn.getInputStream();
final Bitmap bitmap= BitmapFactory.decodeStream(in);
handler.post(new Runnable(){

@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: