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

1

24

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

String DownloadUrl = "http://myexample.com/android/";
     String fileName = "myclock_db.db";

    DownloadDatabase(DownloadUrl,fileName);

    // and the method is

public void DownloadDatabase(String DownloadUrl, String fileName) {
    try {
        File root = android.os.Environment.getExternalStorageDirectory();
        File dir = new File(root.getAbsolutePath() + "/myclock/databases");
        if(dir.exists() == false){
             dir.mkdirs();  
        }

        URL url = new URL("http://myexample.com/android/");
        File file = new File(dir,fileName);

        long startTime = System.currentTimeMillis();
        Log.d("DownloadManager" , "download url:" +url);
        Log.d("DownloadManager" , "download file name:" + fileName);

        URLConnection uconn = url.openConnection();
        uconn.setReadTimeout(TIMEOUT_CONNECTION);
        uconn.setConnectTimeout(TIMEOUT_SOCKET);

        InputStream is = uconn.getInputStream();
        BufferedInputStream bufferinstream = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while((current = bufferinstream.read()) != -1){
            baf.append((byte) current);
        }

        FileOutputStream fos = new FileOutputStream( file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d("DownloadManager" , "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + "sec");
        int dotindex = fileName.lastIndexOf('.');
        if(dotindex>=0){
            fileName = fileName.substring(0,dotindex);

    }
    catch(IOException e) {
        Log.d("DownloadManager" , "Error:" + e);
    }

}

Now the issue is only empty file with filename myclock_db.db is saving in the path. but I need to download and save content of file in the specific folder. Tried several ways to get the file download, but I can't.

Confucius answered 20/4, 2013 at 5:39 Comment(2)
how Can i check in this Example that file Download Completed 100%Dobb
Because your db file can be of a large size, I strongly recommend writing the file in chunks instead of appending all the stream in memory and writing the whole file at once.Dionne
G
15

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.

Genera answered 20/4, 2013 at 5:50 Comment(3)
Okay, How can I edit my code.. can you guide me please..@GeneraConfucius
Actually, The file what I am downloading is a database file which is a file but not an extension of .db. To make it download from server I added extension and get downloaded. Now the file we have downloaded is not accessing as it has an extension. we need to remove extension after download or another way is to declare mime type for file in webconfig which is not working. So, Can you guide me how to remove the extensionafter download. I updated my code for this.. can you check once.Confucius
can anyone Say that in above Example how can i check that file completed successful 100%...Dobb

© 2022 - 2024 — McMap. All rights reserved.