如何使用Downloadmanager类下载/ databases文件夹中的文件?

时间:2014-04-12 09:08:16

标签: android

关于下载管理器here

,有很好的解释

还有一个关于DM here

的好教程

但他们都没有告诉如何下载到不同的目录。如何将我的数据库文件下载到数据库文件夹中。

3 个答案:

答案 0 :(得分:1)

您最好的选择是在您的SD卡中下载您的db 其他地方(即:/temp目录。)
有关如何执行此操作的示例,请访问:http://www.androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification

然后将数据从临时目录中的数据库复制到实际数据库,最后删除下载的数据库。
有关如何执行此操作的示例,请访问:https://stackoverflow.com/a/10371472/2649012

这个不是一个完整的例子,你向正确的方向“踢”。

答案 1 :(得分:1)

试试这个来下载你的文件:

    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();
    }

答案 2 :(得分:0)

DownloadManager.Request对象中使用setDestinationInExternalPublicDir方法更改目的地和名称。

setDestinationUri只做你想做的事。

文档here