Bitmap.decodeFile空指针?报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Bitmap.decodeFile空指针?报错

爱吃鱼的程序员 2020-06-12 11:08:39 38


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

 


Java Android开发
分享到
取消 提交回答
全部回答(1)
  • 爱吃鱼的程序员
    2020-06-12 11:08:56

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

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

    上全错误日志

    ------

     if(out!=null){
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程