开发者社区> 问答> 正文

Bitmap.decodeFile空指针?报错


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命令改权限。

 


展开
收起
爱吃鱼的程序员 2020-06-12 11:08:39 456 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你可以把保存的文件拷出来,用看图软件打开看看,是不是下载文件有问题

    你是说从DDM中的mnt中的sdcard打开,找到此文件,我那个文件夹展不开。还有怎么看这图片有问题。这个图片应该没问题的,原来是差不多是这段代码没报错

    上全错误日志

    ------

     if(out!=null){
    2020-06-12 11:08:56
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载