WebView download opens browser window for a short

2019-03-27 07:09发布

My Problem is that when I hit the pdf link in the webview, the browser opens for a short moment (I guess to download the linked file), the pdf file starts to download and the app returns back to the webview. Is there a way to stop the browser window opening to start the download?

I have used WebViewClient to shouldOverrideUrlLoading so that clicked urls are kept in the webview:

private class LinkWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

And I have a download listener so I can download files (for my needs these are pdf's):

webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);

        }
    });

1条回答
Rolldiameter
2楼-- · 2019-03-27 07:25

Add the MIME type to your Intent you use with startActivity() in your download listener.

查看更多
登录 后发表回答