2012-02-27 30 views
13

Tôi cố gắng tải tệp xuống sdcard trên điện thoại của mình. Trên Android 2.1, 2.2 và 2.3 mọi thứ hoạt động như dự định, nhưng trên Android 4.0 (testen trên trình giả lập và Galaxy Nexus của tôi) nó ném một số FileNotFoundException.FileNotFoundException trên phiên bản Android> 2.3

stacktrace:

02-27 21:49:06.733: W/System.err(27648): java.io.FileNotFoundException: http://www.wdr.de/wdrlive/media/wdr5.m3u 
02-27 21:49:06.733: W/System.err(27648): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) 
02-27 21:49:06.733: W/System.err(27648): at de.arvidg.test.StartActivity$9.run(StartActivity.java:833) 
02-27 21:49:06.733: W/System.err(27648): at java.lang.Thread.run(Thread.java:856) 


Phương pháp của tôi để tải tập tin:

downloadFile("http://www.wdr.de/wdrlive/media/wdr5.m3u"); 

    public void downloadFile(final String fileUrl) { 
     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 

        URL url = new URL(fileUrl); 

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

        urlConnection.setRequestMethod("GET"); 
        urlConnection.setDoOutput(true); 

        urlConnection.connect(); 

//     File cacheDir = appContext.getExternalCacheDir(); 
//     File file = new File("/mnt/sdcard", "/cacheFile.ext"); 
        File SDCardRoot = Environment.getExternalStorageDirectory(); 
        File file = new File(SDCardRoot,"/cacheFile.ext"); 

        if(!file.exists()) file.createNewFile(); 

        FileOutputStream fileOutput = new FileOutputStream(file); 

        InputStream inputStream = urlConnection.getInputStream(); 

        int totalSize = urlConnection.getContentLength(); 
        int downloadedSize = 0; 

        byte[] buffer = new byte[1024]; 
        int bufferLength = 0; 

        while ((bufferLength = inputStream.read(buffer)) > 0) { 
         fileOutput.write(buffer, 0, bufferLength); 
         downloadedSize += bufferLength; 
         Log.d(TAG, "downloadedSize = " + downloadedSize + " | totalSize = " + totalSize); 
//      updateProgress(downloadedSize, totalSize); 

        } 
        fileOutput.close(); 

      } catch (MalformedURLException e) { 
        e.printStackTrace(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

     }).start(); 
    } 
+2

Nó không phải nguyên nhân gây ra sự cố của bạn, nhưng bạn không nên sử dụng tên tệp tuyệt đối cho tệp bộ nhớ cache. Hãy thử thay vào đó: 'File file = new File (SDCardRoot," cacheFile.ext ");' (lưu ý sự vắng mặt của "/"). –

+0

Ok, cảm ơn. Sẽ làm điều đó cho chắc chắn. Bất cứ ai có được ý tưởng, nơi lỗi đến từ đâu? – Leandros

+0

Kiểm tra mã phản hồi urlConnection.getResponseCode(); trước luồng đầu vào quy trình. – yorkw

Trả lời

37

đơn giản loại bỏ urlConnection.setDoOutput(true);

Nhờ PFN @ android-dev #

+3

+1 cho giải pháp đơn giản và sạch sẽ. – yorkw

+0

Câu trả lời hay nhất +1 cho ngắn và thuận tiện Cảm ơn nó đã giải quyết được vấn đề của tôi. –

+0

+1, Làm thế nào về 'setDoInput()'? – NullPointer

8

Vì vậy, để trả lời câu câu hỏi gốc, FileNotFoundException cũng xuất hiện khi bạn nhận được thông báo lỗi từ máy chủ và sau đó thử truy cập getInputStream(). Nếu mã Phản hồi là 400 trở lên, mọi cuộc gọi đến getInputStream() sẽ phát ra lỗi này. Trong trường hợp này, bạn nên kiểm tra getErrorStream() để thay thế. Xem câu trả lời của tôi here.

Các vấn đề liên quan