我的应用是 Cordova 混合应用程序。我安装了 这个 插件,用于从服务器(url)下载 PDF 文件。它在运行 Android 7.0 的设备上工作正常。当相同的应用程序安装在 Oreo 设备上时,什么都不会发生,并且在一段时间后,我看到“下载失败”的消息。可能的原因是什么?
我还升级了一个之前可以使用的移动设备到 8.0 并进行了测试。但是测试失败了。所以在 Oreo 操作系统下它无法工作。
private void startDownload(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
String filename = message.substring(message.lastIndexOf("/")+1, message.length());
try {
filename = URLDecoder.decode(filename,"UTF-8");
} catch (UnsupportedEncodingException e) {
callbackContext.error("Error in converting filename");
}
android.app.DownloadManager downloadManager = (android.app.DownloadManager) cordova.getActivity().getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(message);
android.app.DownloadManager.Request request = new android.app.DownloadManager.Request(Download_Uri);
//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(android.app.DownloadManager.Request.NETWORK_WIFI | android.app.DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(false);
//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle(filename);
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("DataSync File Download.");
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(cordova.getActivity().getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, filename);
//Set visiblity after download is complete
request.setNotificationVisibility(android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
long downloadReference = downloadManager.enqueue(request);
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}