发生FileNotFoundException错误

发生FileNotFoundException错误

package com.imooc.administrator.handlertest;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DownloadActivity extends AppCompatActivity {
    private Handler handler;
    ProgressBar progressBar= (ProgressBar) findViewById(R.id.progressBar);
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);
        /*
        异步下载文件更新进度条的步骤
        主线程
        点击按键
        发起下载
        开启子线程下载
        下载完成后通知主线程
        主线程更新进度条
         */
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //建立子线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        download("http://download.sj.qq.com/upload/connAssitantDownload/upload/MobileAssistant_1.apk 
");
                    }
                }).start();
            }
        });
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 10001:
                        progressBar.setProgress((Integer)msg.obj);
                        break;
                    case 10002:
                }
            }
        };
    }
    private void download(String appUrl) {
        try {
            URL url=new URL(appUrl);
            URLConnection urlconnection=url.openConnection();
            InputStream inputStream=urlconnection.getInputStream();
            //获取文件的总长度
            int contentLength=urlconnection.getContentLength();
            /*
             Environment.getExternalStorageState()获取扩展存储设备的文件目录
             然后创建文件名
             */
            String downloadFolderName= Environment.getExternalStorageState()
                    + File.separator+"imooc"+File.separator;
            //建立新文件夹
            File file=new File(downloadFolderName);
            //判断是否文件夹是否存在
            if(!file.exists()){
                file.mkdir();
            }
            //文件名
            String fileName=downloadFolderName+"imooc.apk";
            //创建文件
            File apkFile=new File(fileName);
            //判断文件是否存在,如存在先删除
            if(apkFile.exists()){
                apkFile.delete();
            }
            if(file!=null){
                Log.i("TAG","文件简历成功");
            }
            int downloanSize=0;//进度数
            //
            byte[] bytes=new byte[1024];
            int length=0;
            //将数据写入fileName
            OutputStream outputStream=new FileOutputStream(fileName);
            while ((length=inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,length);
                downloanSize+=length;
                Message message=Message.obtain();
                message.obj=downloanSize*100/contentLength;
                message.what=10001;
                handler.sendMessage(message);
            }
            //关闭数据
            inputStream.close();
            outputStream.close();
        } catch (MalformedURLException e) {
            Message message2=Message.obtain();
            message2.what=10002;
            handler.sendMessage(message2);
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕雪 2017-04-30 21:34:39

1)查看有没有在Manifest.xml 清单中注册该activity。2)查看Manifest.xml 清单中声明的activity包名与实际包名是否一致

好帮手慕雪 2017-04-27 10:01:10

你好,downloadFolderName的根目录获取错误 ,应该是Environment.getExternalStorageDirectory()而Environment.getExternalStorageState()。祝:学习愉快


  • 提问者 小明2017 #1
    改了还是出现以下错误 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.imooc.administrator.handlertest/com.imooc.administrator.handlertest.DownloadActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access
    2017-04-29 12:03:24
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Android网络操作与数据存储2018版
  • 参与学习           人
  • 提交作业       307    份
  • 解答问题       1613    个

本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师