简体   繁体   English

Android Webview 下载管理器下载 PDF 文件

[英]Android Webview Download manager To Download PDF Files

I have an android application which uses a single WebView which loads a website inside it.我有一个 android 应用程序,它使用单个 WebView 加载一个网站。

setContentView(R.layout.activity_main);
WebView webView = (WebView) this.findViewById(R.id.webview);

Then i used download manager to Download my files from server然后我使用下载管理器从服务器下载我的文件

webView.setDownloadListener(new DownloadListener() {

        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            //   ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
            //for downloading directly through download manager

            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

            request.allowScanningByMediaScanner();
            Environment.getExternalStorageDirectory();
            getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
        }
    });

when i try to download a file which is created dynamically by the website i get to download the websites HTML instead of the PDF.当我尝试下载由网站动态创建的文件时,我会下载网站的 HTML 而不是 PDF。

webView.loadUrl("http://bookboon.com/");

Thanks in Advance提前致谢

Actually that HTML page is showing the login page for authentication.实际上,该 HTML 页面正在显示用于身份验证的登录页面。 So you need a session key which is inside the cookies of that website.因此,您需要一个位于该网站 cookie 内的会话密钥。

So you need to add three lines in onDownloadListener所以需要在onDownloadListener中添加三行

     webView.setDownloadListener(new DownloadListener() {

                public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                    //   ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
                    //for downloading directly through download manager

                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

                 //This three lines will do your work

                    CookieManager cookieManager = CookieManager.getInstance();
                    String cookie = cookieManager.getCookie("<Your website url>");     // which is "http://bookboon.com"
                    request.addRequestHeader("Cookie", cookie);
                 //................................................
                    request.allowScanningByMediaScanner();
                    Environment.getExternalStorageDirectory();
                    getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    dm.enqueue(request);
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM