Android: Downloading in webview not working in oreo, it force closes in Oreo devices

无人久伴 提交于 2019-12-02 12:14:50

问题


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();
        }
    });

回答1:


Try to remove the preloading of fonts by removing

<meta-data 
android:name="preloaded_fonts" 
android:resource="@array/preloaded_fonts" />  

Source Webview in Oreo not working




回答2:


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

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" />

Tutorial for permision prompt

CODE:

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();


        }



    });


来源:https://stackoverflow.com/questions/53120446/android-downloading-in-webview-not-working-in-oreo-it-force-closes-in-oreo-dev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!