这是我的问题: 我现在尝试了10种不同的解决方案,它仍然无法工作。
我想从互联网上下载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
提前致谢:)
答案 0 :(得分:3)
有没有机会,有“WRITE_INTERNAL_STORAGE”作为权限而不是“WRITE_EXTERNAL_STORAGE”我添加了VIA THE PERMISSIONS GUI?我不知道为什么会发生这种情况,但我修复了xml,现在可以了。不管怎样,谢谢你们
答案 1 :(得分:2)
java.io.FileNotFoundException: /mnt/sdcard/Download/speiseplan-rosenheim.pdf:打开失败:EACCES (许可被拒绝)
File targetDir = new File(Environment.getExternalStorageDirectory(), "Download");
尝试
String path = Environment.getExternalStorageDirectory().toString() + "/" + "Download";
和
File file = new File(path, target_filename);