【发布时间】:2017-02-16 02:01:53
【问题描述】:
我想在使用 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
}
【问题讨论】:
-
嘿!为什么不直接重用 databaseFile URI 呢?即databaseFile.toURI();进入 setDestinationUri() 方法。此外,如果您遇到权限问题,您会收到 SecurityException。我很快就适应并尝试了你的例子来下载谷歌的标志,它工作得很好。例如.setDestinationUri(Uri.parse("file:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/google.png"))
-
感谢您的回复!我试过 .setDestinationUri(databaseFile.toURI()) 但它说“android.net.Uri”不能应用于“java.net.Uri”。然后我尝试了 .setDestinationUri(Uri.fromFile(databaseFile)) 但它显示“java.lang.SecurityException: Unsupported path /data/user/0/com.example.laudien.listviewtesting/databases/Employees” 然后我添加了 WRITE_EXTERNAL_STORAGE 和READ_EXTERNAL_STORAGE 权限并在模拟器中接受它,但它是同样的错误。