Download a database file to the app directory

百般思念 提交于 2020-04-10 15:23:08

问题


i want to update the sqlite database of the app when clicking on a button using the DownloadManager.

But it says "java.lang.IllegalArgumentException: Not a file URI: /data/user/0/com.example.laudien.listviewtesting/databases/Employees"

What do i make wrong? Do i need a permission for that? The internet permission is already in the manifest.

Here is the code of my updateDb() method:

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
}

回答1:


Your path /data/user/0/com.example.laudien.listviewtesting is private internal memory of your app. You obtained it using getFilesDir(). Other apps have no access. Including the download manager. Use external memory given by getExternalStorageDirectory() instead.



来源:https://stackoverflow.com/questions/39911001/download-a-database-file-to-the-app-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!