Download file inside WebView Download file inside WebView android android

Download file inside WebView


Have you tried?

mWebView.setDownloadListener(new DownloadListener() {    public void onDownloadStart(String url, String userAgent,                String contentDisposition, String mimetype,                long contentLength) {        Intent i = new Intent(Intent.ACTION_VIEW);        i.setData(Uri.parse(url));        startActivity(i);    }});

Example Link: Webview File Download - Thanks @c49


Try this out. After going through a lot of posts and forums, I found this.

mWebView.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.allowScanningByMediaScanner();            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");            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();        }    });

Do not forget to give this permission! This is very important! Add this in your Manifest file(The AndroidManifest.xml file)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />        <!-- for your file, say a pdf to work -->

Hope this helps.Cheers :)


    mwebView.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.setMimeType(mimeType);    String cookies = CookieManager.getInstance().getCookie(url);    request.addRequestHeader("cookie", cookies);    request.addRequestHeader("User-Agent", userAgent);    request.setDescription("Downloading file...");    request.setTitle(URLUtil.guessFileName(url, contentDisposition,            mimeType));    request.allowScanningByMediaScanner();    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_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",            Toast.LENGTH_LONG).show();}});