这是我的问题:
我现在尝试了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
提前致谢 :)
最佳答案
是否有可能是我通过PERMISSIONS GUI添加的“WRITE_INTERNAL_STORAGE”而不是“WRITE_EXTERNAL_STORAGE”的权限?我不知道为什么会这样,但是我修复了xml,现在可以正常工作了。还是谢谢你们
关于java - FileNotFoundException on ExternalStorage,尽管我有权读写,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10528717/