How i can enable to Download PDF via Webview

前端 未结 1 709
一整个雨季
一整个雨季 2020-12-21 07:04

i am very new to Android but i create a webview and it displays a wordpress page with a Download Link for a PDF in it, but when i tap on it nothing happends, i hope you can

相关标签:
1条回答
  • 2020-12-21 07:26

    Try to add this to to your code: (probably in your updateWebView method) This will intercept any downloads and send them to the standard android download manager.

     mWebView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
                    Request request = new Request( Uri.parse(url));
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myPDFfile.pdf"); 
                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    dm.enqueue(request);        
       }
    });
    

    Note that this will use the same filename everytime, so your file may get replaced unless you modify the code to use a different filename.

    0 讨论(0)
提交回复
热议问题