2011-12-31 21 views
3

アンドロイドメモリディスクに保存し、私は私の間違っているかを理解カント、私のコードがあるURLからファイルをダウンロードし、私はURLからファイルをダウンロードし、メモリカートに保存しようとしてい

URL url = new URL(imageURL); 
     File file = new File(fileName); 

     long startTime = System.currentTimeMillis(); 
     Log.d("ImageManager", "download begining"); 
     Log.d("ImageManager", "download url:" + url); 
     Log.d("ImageManager", "downloaded file name:" + fileName); 
     URLConnection ucon = url.openConnection(); 
     InputStream is = ucon.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 

     ByteArrayBuffer baf = new ByteArrayBuffer(50); 
     int current = 0; 
     while ((current = bis.read()) != -1) { 
      baf.append((byte) current); 
     } 

     /* Convert the Bytes read to a String. */ 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(baf.toByteArray()); 
     fos.close(); 
     Log.d("ImageManager", "download ready in" 
         + ((System.currentTimeMillis() - startTime)/1000) 
         + " sec"); 

が、私は実際に私は誰もが助けてください、私は何を行うことができます知らない私のコンソールに

12-27 14:50:01.302: D/EXCEPTION GET:(8062): java.io.FileNotFoundException: /my.jpg (Read-only file system) 

を次のエラーを見つけました。

答えて

3

ファイルを保存するパスに問題があります。ファイル名は/で始まり、コードにこの変更を適用しないでください。

File extStore = Environment.getExternalStorageDirectory(); 
File file = new File(extStore, fileName); 

あなたcheck media availabilityべきで保存しようとする前に。

+0

このhttp:// stackoverflow.com/ questions/8687849/how-can-i-download-a-file-to-my-downloads-folder-androidを見てください。 – Pritom

関連する問題