How to download a file from a server and save it in specific folder in SD card in Android? How to download a file from a server and save it in specific folder in SD card in Android? android android

How to download a file from a server and save it in specific folder in SD card in Android?


Your download URL is not a link to any file. It's a directory. Make sure its a file and exists. Also check your logcat window for error logs. One more suggestion, its always better to do a printStackTrace() in catch blocks instead of Logs. Its gives a more detailed view of the error.

Change this line:

    URL url = new URL("http://myexample.com/android/");

to:

    URL url = new URL("http://myexample.com/android/yourfilename.txt"); //some file url

Next, in catch block, add this line:

e.printStackTrace();

Also in the directory path, it should be something like this:

File dir = new File(root.getAbsolutePath() + "/mnt/sdcard/myclock/databases");

instead of

File dir = new File(root.getAbsolutePath() + "/myclock/databases");

Next, make sure you have acquired permission for writing to external storage in Android manifest.