package com.example.thread_demo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.widget.ImageView;
public class BitmapThread extends Thread {
private String url;
private ImageView imageView;
private Handler handle;
public BitmapThread(ImageView imageView, String url, Handler handler) {
this.url = url;
this.handle = handler;
this.imageView = imageView;
}
@Override
public void run() {
try {
URL HttpUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) HttpUrl.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
InputStream in = conn.getInputStream();
FileOutputStream out = null;
File downloadFile = null;
String fileName = String.valueOf(System.currentTimeMillis());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File parent = Environment.getExternalStorageDirectory();
downloadFile = new File(parent, fileName);
out = new FileOutputStream(downloadFile);
}
byte[] b = new byte[2 * 1024];
int len;
if (out != null) {
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
}
final Bitmap bitmap = BitmapFactory.decodeFile(downloadFile.getAbsolutePath());
handle.post(new Runnable() {
public void run() {
imageView.setImageBitmap(bitmap);
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我的代码,为什么运行之后是空白的,没有图片啊?