Android:在 webview 中下载在 oreo 中不起作用,它在 Oreo 设备中强制关闭 [英] Android: Downloading in webview not working in oreo, it force closes in Oreo devices

查看:55
本文介绍了Android:在 webview 中下载在 oreo 中不起作用,它在 Oreo 设备中强制关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下载管理器代码在 Android WebView 中不适用于 Oreo 设备,但适用于旧版本如果不是奥利奥设备,它会吐司下载文件"并下载,但在奥利奥的情况下它会强制关闭(崩溃)

Download manager code is not working in Android WebView for Oreo devices, but it's working well for older versions In case other than Oreo devices it toasts "downloading file" and it gets downloaded but in case of Oreo it force closes (crash)

下面是我使用的代码(片段)

Below is the code I am using (in fragment)

 webView.setDownloadListener(new DownloadListener() {
        @Override

        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {

            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!

            DownloadManager dm = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getActivity().getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
                    Toast.LENGTH_LONG).show();
        }
    });

推荐答案

这是我想出来并为我工作的代码.

This is the code what I figured it out and working for me.

注意:代码是在一个片段中执行的,对于主活动删除 getActivity().在清单中给予许可

NOTE: code is been executed in a fragment, for the main activity remove getActivity(). in manifest give the permission

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

教程权限提示

代码:

webView.setDownloadListener(new DownloadListener() {

        @Override

        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            if(Build.VERSION.SDK_INT <= 26 ){
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
            }
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!

            DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getActivity().getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
                    Toast.LENGTH_LONG).show();


        }



    });

这篇关于Android:在 webview 中下载在 oreo 中不起作用,它在 Oreo 设备中强制关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆