public class HttpThread extends Thread {
private String url;
private WebView webView;
private Handler handler;
private 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.url = url;
this.imageView = imageView;
this.handler = handler;
}
@Override
public void run() {
try {
URL httpUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.getDoInput();
InputStream in = connection.getInputStream();
FileOutputStream out = null;
File filename = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File dir = Environment.getExternalStorageDirectory();
filename = new File(dir,"tupian");
out = new FileOutputStream(filename);
}
byte[] buf = new byte[2*1024];
int len;
if (out!=null){
while ((len = in.read(buf))!=-1){
out.write(buf,0,len);
}
}
final Bitmap bitmap = BitmapFactory.decodeFile(filename.getAbsolutePath());
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}