2016-10-07 62 views
1

我想在使用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 
} 
+0

嘿!爲什麼不直接重用databaseFile URI?即,databaseFile.toURI();進入setDestinationUri()方法。 另外,如果你有權限問題,你會得到一個SecurityException。我很快適應並嘗試了您的示例來下載Google的徽標,並且它工作得很好。例如.setDestinationUri(Uri.parse(「file:」+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+「/google.png」)) –

+0

感謝您的回覆!我嘗試.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

回答

1

你的路徑/data/user/0/com.example.laudien.listviewtesting是你的應用程序的私有內存。你使用getFilesDir()獲得了它。其他應用程序無法訪問。包括下載管理器。改爲使用getExternalStorageDirectory()給出的外部存儲器。

相關問題