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();
}
}
}
HttpURLConnection从网上下载图片
最新推荐文章于 2023-08-24 11:55:28 发布