3

ここに私の問題があります:私は今10の異なる解決策を試しましたが、それでもうまくいきません.

インターネットから PDF ファイルをダウンロードして、外部ストレージに保存したいと考えています。

私がやったこと: - INTERNET と WRITE_EXTERNAL_STORAGE の「Uses Permissions」を追加しました - エミュレーターで試しただけでなく、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

前もって感謝します :)

4

2 に答える 2

3

「WRITE_EXTERNAL_STORAGE」の代わりに権限として「WRITE_INTERNAL_STORAGE」があった可能性はありますか?私はPERMISSIONS GUI経由で追加しましたか? なぜそうなったのかはわかりませんが、xml を修正したところ、動作するようになりました。とにかくありがとう

于 2012-05-12T07:07:41.617 に答える
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);
于 2012-05-10T08:28:06.467 に答える