android - 下载管理器在 Android Oreo 中不起作用

标签 android cordova android-download-manager download-manager

我的是一个 cordova 混合应用程序。我已经安装了this从服务器(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.");
        }
    }

最佳答案

确保您已在 Manifest.xml 文件中添加此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and use the following code

 DownloadManager.Request request = new DownloadManager.Request(
            Uri.parse(url));

    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
    request.setDestinationInExternalPublicDir(
            Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                    url, contentDisposition, mimeType));
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    dm.enqueue(request);
    Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
            Toast.LENGTH_LONG).show();

关于android - 下载管理器在 Android Oreo 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52075698/

相关文章:

android - 在 mp4Parser 的帮助下修剪视频

java - 将一个 Android 应用程序导入到另一应用程序中

java - JSOUP 无法在 Android Studio 中提取动态生成的 DOM 内容

android - 创建android项目后无法导入phonegap项目

android - IntentService 中的 registerReceiver 未命中 BroadcastReceiver

android - 从android中的downloadManager查找下载完成

android - 给Android中Viewpager的PagerTabStrip添加样式

javascript - 如何调试phonegap windows phone 8.1

android - 如何让 PhoneGap Facebook Connect 插件正常工作

java - 如何循环ArrayList并将每个值发送到下载管理器?