FileNotFoundException
代码:
package com.example.handledemo; import android.os.Build; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class DownloadActivity extends AppCompatActivity { private Button btn; private ProgressBar downloadBar; private String downloadURL = "http://download.sj.qq.com/upload/connAssitantDownload/upload/MobileAssistant_1.apk"; private final int DOWNLOAD_CODE = 1001; private int progress; private Handler handler; private String TAG = "TAG"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_download); initView(); btn.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.N) @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { download(downloadURL); } }).start(); } }); findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File file = new File("/storage/emulated/0/imooc/imooc.apk"); } }); handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case DOWNLOAD_CODE: downloadBar.setProgress(msg.arg1); break; } } }; } @RequiresApi(api = Build.VERSION_CODES.N) private void download(String downloadURL) { try { URL url = new URL(downloadURL); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); String fileName = Environment.getExternalStorageDirectory() + File.separator + "imooc.apk"; File file = new File(fileName); if (file != null) { Log.i(TAG, "download: 文件建立成功"); } progress = 0; int len = 0; long fileSize = con.getContentLengthLong(); long downloadSize = 0; byte[] bytes = new byte[1024]; //发生异常FileNotFoundException OutputStream out = new FileOutputStream(file); while ((len = in.read(bytes)) != -1) { out.write(bytes, 0, len); downloadSize += len; progress = Math.round(downloadSize / fileSize * 100); Message message = Message.obtain(); message.what = DOWNLOAD_CODE; message.arg1 = progress; handler.sendMessage(message); } in.close(); out.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void initView() { btn = (Button) findViewById(R.id.btn); downloadBar = (ProgressBar) findViewById(R.id.downloadBar); } }
错误信息:
04-05 05:06:54.415 9389-10719/com.example.handledemo W/System.err: java.io.FileNotFoundException: /storage/emulated/0/imooc.apk (Permission denied) 04-05 05:06:54.416 9389-10719/com.example.handledemo W/System.err: at java.io.FileOutputStream.open(Native Method) 04-05 05:06:54.416 9389-10719/com.example.handledemo W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221) 04-05 05:06:54.416 9389-10719/com.example.handledemo W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:169) 04-05 05:06:54.416 9389-10719/com.example.handledemo W/System.err: at com.example.handledemo.DownloadActivity.download(DownloadActivity.java:91) 04-05 05:06:54.417 9389-10719/com.example.handledemo W/System.err: at com.example.handledemo.DownloadActivity.access$100(DownloadActivity.java:26) 04-05 05:06:54.417 9389-10719/com.example.handledemo W/System.err: at com.example.handledemo.DownloadActivity$1$1.run(DownloadActivity.java:50) 04-05 05:06:54.417 9389-10719/com.example.handledemo W/System.err: at java.lang.Thread.run(Thread.java:761)
AndroidManifest:
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"></uses-permission>
46
收起
正在回答
2回答
在发生异常之前加一个判断,看看file对象是否为null
Android网络操作与数据存储2018版
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星