下载一个数据库文件到app目录

P1x*_*ler 6 java sqlite android

我想在使用 DownloadManager 单击按钮时更新应用程序的 sqlite 数据库。

但它说“java.lang.IllegalArgumentException:不是文件URI:/data/user/0/com.example.laudien.listviewtesting/databases/Employees”

我做错了什么?我需要许可吗?互联网权限已在清单中。

这是我的 updateDb() 方法的代码:

private void updateDb() {
    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); // create download manager
    DownloadFinishedReceiver receiver = new DownloadFinishedReceiver(); // create broadcast receiver
    registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); // register the receiver
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DATABASE_URL)); // create a download request

    // delete database file if it exists
    File databaseFile = new File(getDatabasePath(DATABASE_NAME).getAbsolutePath());
    if(databaseFile.exists())
        databaseFile.delete();

    request//.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN) // not visible
        .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI) // only via wifi
        .setDestinationUri(Uri.parse("file:" + getDatabasePath(DATABASE_NAME).getAbsolutePath())); // set path in app dir
    downloadManager.enqueue(request); // enqueue the download request
}
Run Code Online (Sandbox Code Playgroud)

gre*_*pps 6

您的路径/data/user/0/com.example.laudien.listviewtesting是您的应用程序的私有内部存储器。您使用getFilesDir(). 其他应用程序无法访问。包括下载管理器。改用由提供的外部存储器getExternalStorageDirectory()