下载一个数据库文件到app目录 [英] Download a database file to the app directory
问题描述
我想在使用 DownloadManager 单击按钮时更新应用程序的 sqlite 数据库.
i want to update the sqlite database of the app when clicking on a button using the DownloadManager.
但它说java.lang.IllegalArgumentException:不是文件 URI:/data/user/0/com.example.laudien.listviewtesting/databases/Employees"
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.
这是我的 updateDb() 方法的代码:
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
}
推荐答案
你的路径 /data/user/0/com.example.laudien.listviewtesting
是你的应用的私有内存.您使用 getFilesDir()
获得它.其他应用程序无法访问.包括下载管理器.改用 getExternalStorageDirectory()
提供的外部存储器.
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.
这篇关于下载一个数据库文件到app目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!