13

I try to download a file to sdcard on my phone. On Android 2.1, 2.2 and 2.3 everything works like intended, but on Android 4.0 (testen on emulator and my Galaxy Nexus) it throws a FileNotFoundException.

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)


My method for downloading file:

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();
    }
Leandros
  • 16,805
  • 9
  • 69
  • 108
  • 2
    It's not what's causing your problem, but you should not be using an absolute file name for the cache file. Try this instead: `File file = new File(SDCardRoot,"cacheFile.ext");` (note the absence of "/"). – Ted Hopp Feb 27 '12 at 21:24
  • Ok, thanks. Will do it for sure. Anyone get idea where the error comes from? – Leandros Feb 27 '12 at 21:30
  • Check the response code urlConnection.getResponseCode(); before process input stream. – yorkw Feb 27 '12 at 21:48
  • Nice. Thanks. 405 - Method not allowed. Is there another download message ("GET" dont seems to work). – Leandros Feb 27 '12 at 22:01
  • Try to comment this string urlConnection.setRequestMethod("GET"); in your code. – Yury Feb 27 '12 at 22:04
  • Yury: The same error. `urlConnection.getRequestMethod()` returns only `POST`. I think I have to live, that I cant get the file. Is it possible to open file and get content? – Leandros Feb 27 '12 at 22:06
  • The weird thing is, that it work on Android 2.2. :D – Leandros Feb 27 '12 at 22:12
  • 1
    Some one suggest using apache httpClient can fix it,not sure if it works in your case here ICS, worth to [check it out](http://stackoverflow.com/questions/4218807/android-filenotfound-exception-cannot-getinputstream-from-image-url-that-does) – yorkw Feb 27 '12 at 22:24

2 Answers2

37

Simple remove urlConnection.setDoOutput(true);

Thanks to pfn @ #android-dev

Leandros
  • 16,805
  • 9
  • 69
  • 108
8

So to answer the original question, a FileNotFoundException also comes when you get an error message from the server and then try to access getInputStream(). If the Response code is a 400 or above, any call to getInputStream() will throw this error. In this case, you should check getErrorStream() instead. See my answer here.

Community
  • 1
  • 1
Stefan Anca
  • 1,386
  • 14
  • 23