The following functions fetches the file contents from a remote location, stores it in a given directory with a given file name on the SDCard of the device.

public static void downloadFileToSDCard
(String fileURL, String directory,String fileName)
throws Exception {


    Log.d("download file : " + fileURL);

    // mark the directory
    String RootDir = Environment.getExternalStorageDirectory() + File.separator + directory;
    File RootFile = new File(RootDir);
    RootFile.mkdir();

    Log.d("directory made or already there");

    // get the file contents
    URL u = new URL(fileURL);
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    Log.d("connected");

    FileOutputStream f = new FileOutputStream(new File(RootFile, fileName));
    InputStream in = c.getInputStream();
    byte[] buffer = new byte[1024];
    int len1 = 0;
    Log.d("got stream reading");

    // read file contents
    while ((len1 = in.read(buffer)) > 0) {
        Log.d("write read to buffer");
        f.write(buffer, 0, len1);
    }

    // close the stream
    f.flush();
    f.close();
    in.close();
}