如何直接从 Android WebView 触发文件下载,以便经过身份验证的用户不必再次登录?



用户通过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查看器,它会立即切换到它。

我通过实现下载监听器和传递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);
              }
        }
    });

的答案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);
    }
});

相关内容

最新更新