这是我的问题:我现在尝试了 10 种不同的解决方案,但仍然无法正常工作。
我想从 Internet 下载 PDF 文件,然后将其保存到外部存储中。
我所做的: - 添加了 INTERNET 和 WRITE_EXTERNAL_STORAGE “使用权限” - 尝试使用模拟器,以及插入和关闭的 Galaxy Nexus
那是我的 doInBackground 下载方法:
26 @Override
27 protected String doInBackground(String... downloadUrl) {
28 if (!mediaIsAvailable()) return null;
29
30 final int BUFFER_SIZE = 1024 * 23;
31 String url = downloadUrl[0];
32 String target_filename = downloadUrl[1];
33 String ns = Context.NOTIFICATION_SERVICE;
34 NotificationManager notificationManager = (NotificationManager) context.getSystemService(ns);
35
36 try {
37 File targetDir = new File(Environment.getExternalStorageDirectory(), "Download");
38 if(!targetDir.exists())
39 {
40 targetDir.mkdirs();
41 }
42
43 File file = new File(targetDir, target_filename);
44 URL urlObj = new URL(url);
45 URLConnection connection = urlObj.openConnection();
46
47 BufferedInputStream bis = new BufferedInputStream(connection.getInputStream(), BUFFER_SIZE);
48
49 FileOutputStream fos = new FileOutputStream(file);
50 byte[] bArray = new byte[BUFFER_SIZE];
51 int current = 0;
52 int read = 0;
53 while(current != -1)
54 {
55 fos.write(bArray,0,current);
56 current = bis.read(bArray, 0, BUFFER_SIZE);
57 read = read + current;
58 }
59 fos.close();
60 bis.close();
61 notificationManager.cancel(1);
62 } catch (MalformedURLException e) {
63 e.printStackTrace();
64 } catch (FileNotFoundException e) {
65 e.printStackTrace();
66 } catch (IOException e) {
67 e.printStackTrace();
68 }
69 return null;
70 }
这就是我得到的例外:
05-10 08:31:17.861: W/System.err(23054): java.io.FileNotFoundException: /mnt/sdcard/Download/speiseplan-rosenheim.pdf: open failed: EACCES (Permission denied)
05-10 08:31:17.861: W/System.err(23054): at libcore.io.IoBridge.open(IoBridge.java:406)
05-10 08:31:17.861: W/System.err(23054): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
05-10 08:31:17.861: W/System.err(23054): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
05-10 08:31:17.861: W/System.err(23054): at de.fhrosenheim.app.campus.helpers.DownloadFileHelper.doInBackground(DownloadFileHelper.java:49)
05-10 08:31:17.861: W/System.err(23054): at de.fhrosenheim.app.campus.helpers.DownloadFileHelper.doInBackground(DownloadFileHelper.java:1)
05-10 08:31:17.861: W/System.err(23054): at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-10 08:31:17.861: W/System.err(23054): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-10 08:31:17.861: W/System.err(23054): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-10 08:31:17.861: W/System.err(23054): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-10 08:31:17.861: W/System.err(23054): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-10 08:31:17.868: W/System.err(23054): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-10 08:31:17.868: W/System.err(23054): at java.lang.Thread.run(Thread.java:856)
05-10 08:31:17.868: W/System.err(23054): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
05-10 08:31:17.868: W/System.err(23054): at libcore.io.Posix.open(Native Method)
05-10 08:31:17.868: W/System.err(23054): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-10 08:31:17.868: W/System.err(23054): at libcore.io.IoBridge.open(IoBridge.java:390)
05-10 08:31:17.868: W/System.err(23054): ... 11 more
提前致谢 :)