Java – Download on Android – FileNotFoundException But the file exists

Download on Android – FileNotFoundException But the file exists… here is a solution to the problem.

Download on Android – FileNotFoundException But the file exists

I

tried downloading a file from a server in Android, but all I got was a FileNotFoundException.

My download method works for all the files I provide, but not for this specific URL.
The URL that doesn’t work looks like this: http://server.com/download.aspx?action=geta&sid=12345&file=somedata.xml&client=android

I

see no error in the url and if I enter it into a web browser, I get the file immediately. Permissions are also ok, I can download all other files from the server in this way, e.g. with “getfile” instead of “get”, but the file I want to download is only accessible through “get”, so I can’t just replace it.

Anyway, here’s how I downloaded it:

public String downloadURL (URL url, String fileName) throws IOException {
    String destination = null;

try {           
        destination = Environment.getExternalStorageDirectory() + "/" 
                + "myapp/" + fileName;

InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(destination);

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

while ((bytes = input.read(buffer)) != -1) {
            output.write(buffer,0, bytes);
        }

output.flush();
        output.close();
        input.close();

}
    catch (Exception e) {
        Log.e("DOWNLOAD URL", "downloading file failed due to " + e.toString());
    }

return destination; 
}

I’m a little desperate here, I searched on StackOverflow for a while but didn’t find a solution, thank you so much for your help.

EDIT: Error stack, if yes I think it is:

08-16 17:32:58.530: WARN/System.err(24754): java.io.FileNotFoundException: http://myfirm.com/download.aspx?action=get&sid=6d62429x-4e1c&file=data.xml&client=android
08-16 17:32:58.530: WARN/System.err(24754): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:532)
08-16 17:32:58.530: WARN/System.err(24754): at java.net.URL.openStream(URL.java:645)
08-16 17:32:58.530: WARN/System.err(24754): at myapp.cont.fetch.Downloader.downloadURL(Downloader.java:412)
08-16 17:32:58.530: WARN/System.err(24754): at myapp.cont.fetch.Downloader.getData(Downloader.java:465)
08-16 17:32:58.530: WARN/System.err(24754): at myapp.cont.fetch.Downloader$GetFilesTask.doInBackground(Downloader.java:245)
08-16 17:32:58.530: WARN/System.err(24754): at myapp.cont.fetch.Downloader$GetFilesTask.doInBackground(Downloader.java:1)
08-16 17:32:58.530: WARN/System.err(24754): at android.os.AsyncTask$2.call(AsyncTask.java:252)
08-16 17:32:58.530: WARN/System.err(24754): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-16 17:32:58.530: WARN/System.err(24754): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-16 17:32:58.530: WARN/System.err(24754): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-16 17:32:58.530: WARN/System.err(24754): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-16 17:32:58.530: WARN/System.err(24754): at java.lang.Thread.run(Thread.java:1020)

Solution

The problem is with your target file. The directory myapp does not exist on SDCard, or you do not include write permissions on SD card in the manifest:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Related Problems and Solutions