4

用户通过 WebView 登录到我的应用程序内的移动页面。我正在使用以下代码从 WebView 请求中捕获可下载资源并将其作为意图传递:

webView.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);
            }
        });

在模拟器中,这实际上打开了 Android 浏览器,然后要求用户再次登录,此时它开始下载文件。

有没有办法可以直接从我的 WebView 触发下载,这样用户就不必再次登录?默认情况下,如果不添加此 DownloadListener,则什么也不会发生。

理想情况下,一旦文件被下载,我想触发文件的意图,所以如果用户有一个 PDF 查看器,它会立即切换到它。

4

2 回答 2

2

我通过实现下载监听器并传递 cookie 来解决这个问题:

this.setDownloadListener(new DownloadListener() {

        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long len) {
            if ( null != url && (url.endsWith(".pdf") || url.endsWith(".pptx"))) {
                Uri source = Uri.parse(url);
                int lastSEP = url.lastIndexOf("//");
                String DL_to_filename = url.substring( lastSEP+1 );
                DownloadManager.Request request = new DownloadManager.Request(source);
                request.setDescription("requested " + url +" downloading");
                request.setTitle(DL_to_filename);
                String cookies = CookieManager.getInstance().getCookie(url);
                request.addRequestHeader("cookie", cookies);
                request.addRequestHeader("User-Agent", userAgent);
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, DL_to_filename);
                request.setVisibleInDownloadsUi(true);
                request.setMimeType(mimetype);

                DownloadManager manager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);                   

                manager.enqueue(request);
              }
        }
    });
于 2015-12-14T23:53:46.850 回答
0

https://stackoverflow.com/users/5680028/jihshin的答案非常适合我但是我的 url 没有包含.pdf,即使是 pdf 所以我只是删除了 if 语句,一切都很完美。

this.setDownloadListener(new DownloadListener() {

    public void onDownloadStart(String url, String userAgent,
                                String contentDisposition, String mimetype,
                                long len) {

            Uri source = Uri.parse(url);
            int lastSEP = url.lastIndexOf("//");
            String DL_to_filename = url.substring( lastSEP+1 );
            DownloadManager.Request request = new DownloadManager.Request(source);
            request.setDescription("requested " + url +" downloading");
            request.setTitle(DL_to_filename);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, DL_to_filename);
            request.setVisibleInDownloadsUi(true);
            request.setMimeType(mimetype);

            DownloadManager manager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);                   

            manager.enqueue(request);

    }
});
于 2020-05-24T05:19:49.220 回答