0

Well there are good explanation about Download manager here

And also there is a good tutorial about DM here

But both of them didnt tell how to download into different directory. how can download my database file into the databases folder.

Community
  • 1
  • 1
mehmet
  • 1,558
  • 5
  • 30
  • 41

3 Answers3

1

Your best bet is to download your db somewhere else (i.e.: a /temp directory in your sd card).
An example on how to do this can be found here: http://www.androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification

Then copy data from the db in that temporary directory to your actual db and finally delete the downloaded db.
An example on how to do that is found here: https://stackoverflow.com/a/10371472/2649012

This one isn't a complete example, youst a "kick" into the right direction.

Community
  • 1
  • 1
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I edited my answer to show you a couple of key example codes. The second isn't a complete example, youst a "kick" into the right direction. – Phantômaxx Apr 12 '14 at 11:34
  • How can I found my download file in `Environment.DIRECTORY_DOWNLOADS` – mehmet Apr 12 '14 at 11:37
1

try this to download your file:

    try {
        URL url = new URL(" <URL HERE> ");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        String DatbasesPath = Environment.getDataDirectory() + "/data/" + getApplication().getPackageName() + "/databases/";
        File file = new File(DatbasesPath, "mydb.db");
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
The Badak
  • 2,010
  • 2
  • 16
  • 28
0

In your DownloadManager.Request object use setDestinationInExternalPublicDir method to change destination and name.

Or setDestinationUri which does only what you want.

Documentation here.

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53