WebView download opens browser window for a short moment before returning to WebView
12,387
Add the MIME type to your Intent
you use with startActivity()
in your download listener.
Author by
qubz
Updated on June 14, 2022Comments
-
qubz over 1 year
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); } });
-
qubz almost 13 yearsThanks for your reply. I see mimetype as the parameter for onDownloadStart, though I'm not sure how to use it in the body of the function. Ok, scratch that, I think I got it: intent.setType(MIME_TYPE_PDF);
-
qubz almost 13 yearsAfter adding the above line, I click the file link and as I have Adobe PDF Reader installed, it opens. But it seems the file has not been downloaded as it does not appear in the download folder or the pdf reader list.
-
CommonsWare almost 13 years@qubit: If you want to download it, you need to download it. This is no different than, say, viewing a PDF in IE or Chrome -- it comes up in the browser alone, and you need to take extra steps to actually save it. In the desktop browsers, the PDF plugin has a Save As option for this. I do not know if the Adobe Reader or other PDF viewers offer a similar facility (e.g., via the options menu). You may be better served doing the download yourself, storing it wherever you want it stored, then doing
ACTION_VIEW
on your stored copy. -
qubz almost 13 yearsFor clarity, are you saying that there is no way to download the file in the webview without opening up the browser for a brief second? If I perform the same action in the browser, the file downloads from the same page I am viewing. There is no need for the browser to open a new blank tab/page like the webview does to download it.
-
CommonsWare almost 13 years@qubit: When I say "you need to download it", I mean by using
HttpClient
and doing so from Java. "If I perform the same action in the browser, the file downloads from the same page I am viewing." -- that is because the browser authors wrote Java code and downloaded it viaHttpClient
or something, presumably. All I can tell you is thatACTION_VIEW
does not imply that an HTTP resource is permanently downloaded -- if that were the case, your phone would have run out of storage months ago. If you want the HTTP resource to be permanently downloaded, useHttpClient
and download it. -
qubz almost 13 yearsCheers, thanks for the explanation and help. I'll give it a shot :-)
-
CoronaPintu over 9 years@CommonsWare it's open browser for dowloading but i want to download in my own browser and i have no extendion or mime type i am dowloading pdf file liek on github i click on dowload but it automaticaly start downloading in that way my file is dowloading in browser.
-
Rohit about 5 years@CommonsWare am able to download simple pdf but when it comes to secured websites, download listener never gets called. Please look at my question stackoverflow.com/questions/51929878/…