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.
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.
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();
}
In your DownloadManager.Request
object use setDestinationInExternalPublicDir method to change destination and name.
Or setDestinationUri
which does only what you want.
Documentation here.