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

后端 未结 1 477
名媛妹妹
名媛妹妹 2020-11-30 01:08

I have one requirement in my Android application. I need to download and save file in specific folder of SD card programmatically. I have developed source code, which is

相关标签:
1条回答
  • 2020-11-30 01:11

    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.

    0 讨论(0)
提交回复
热议问题