public class HttpThread extends Thread { private String url; private Handler handler; private WebView webView; 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;
}
public void run(){
try {
URL httpUrl = new URL(url);
try {
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
InputStream in = conn.getInputStream();
File downloadFile = null;
FileOutputStream out = 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());
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
imageView.setImageBitmap(bitmap);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
报错空指针,权限已经加过了,整个error级别全是空指针的
final Bitmap bitmap = BitmapFactory.decodeFile(downloadFile.getAbsolutePath());
debug过了,downloadFile为空,求解咋办。
本人ADT上的外置存储卡打不开,不知道是不是要adb命令改权限。
你可以把保存的文件拷出来,用看图软件打开看看,是不是下载文件有问题
你是说从DDM中的mnt中的sdcard打开,找到此文件,我那个文件夹展不开。还有怎么看这图片有问题。这个图片应该没问题的,原来是差不多是这段代码没报错上全错误日志
------
if(out!=null){版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。