DownloadManagerを使用してボタンをクリックすると、アプリケーションのsqliteデータベースを更新します。データベースファイルをアプリケーションディレクトリにダウンロードします。
しかし、それは "java.lang.IllegalArgumentExceptionが/data/user/0/com.example.laudien.listviewtesting/databases/Employees:未ファイルURI" と言い、私は間違って何をするん
?私はその許可が必要ですか?インターネットの許可は既にマニフェストにあります。
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
}
Hey!なぜ直接databaseFile URIを直接再利用しないのですか?すなわち、databaseFile.toURI(); setDestinationUri()メソッドに追加します。 さらに、アクセス権の問題があった場合は、SecurityExceptionが発生します。私はすぐにあなたの例をGoogleのロゴをダウンロードするように適応させて試しました。例えば.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:サポートされていないパス/data/user/0/com.example.laudien.listviewtesting/databases/Employeesを" と言う私は、 WRITE_EXTERNAL_STORAGEおよびREAD_EXTERNAL_STORAGE権限を追加し、エミュレータでそれを受け入れましたが、これは同じエラーです。 – Norman