如何从Android WebView中直接触发文件下载,让已认证的用户无需二次登录?

问题描述 投票:3回答:2

用户通过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浏览器,它将立即切换到它。

android-intent download android-webview
2个回答
1
投票

我通过实现下载监听器和传递Cookies来解决这个问题。

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

0
投票

答案是 https:/stackoverflow.comusers5680028jihshin。 工作对我来说非常好,但我的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);

    }
});
© www.soinside.com 2019 - 2024. All rights reserved.