1

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();
        }
    });
  • Hi, please refrain from using the word "urgent" in question titles as it may end up having the opposite effect - See also: [Under what circumstances may I add “urgent” or other similar phrases to my question](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) – Michael Dodd Nov 02 '18 at 14:32
  • Okay thank you for bringing it into my notice – Kàrthîk ßàlàkrìshña Nov 02 '18 at 14:46
  • 1
    Also, can you [edit] the question and expand on what "_is not working_" means? Does it: not do anything (noticeable)? download a partial file? crash your app? crash Android? Do you have a `logcat` you can show? – TripeHound Nov 02 '18 at 15:03
  • Logcat? Or at least wrap your code in `try...catch` and see if there's an exception being thrown. I'm not an Android expert, but my first guess would be something to do with permissions has changed, either permission to download, and/or to write to wherever you're storing the download. – TripeHound Nov 02 '18 at 15:10
  • Permission is granted, ill post the logcat – Kàrthîk ßàlàkrìshña Nov 02 '18 at 15:13

2 Answers2

0

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

AbA2L
  • 110
  • 7
0

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


        }



    });