I have an Android App with an Webview activity that shows some embedded reports. These can be exported in various formats (Excel, csv, pdf, text). The issue is that when I download these reports on my browser (or the chrome browser on my phone) they download fine. When I do it from the webview (same url) the files download but size is always 0 bytes.
Any ideas of what could be the reason for this?
Here is relevant code on the activity:
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(BackendConfig.URL_MOBILE_FINNISH)) {
finish();
Intent i = new Intent(WebViewActivity.this, BaseActivity.class);
startActivity(i);
return true;
} else {
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimetype);
//------------------------COOKIE!!------------------------
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
//------------------------COOKIE!!------------------------
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}
});
return false;
}
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setAllowFileAccess(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(false);
webSettings.setDefaultTextEncodingName("utf-8");
webView.addJavascriptInterface(new GetPassword(), "GetPassword");
webView.addJavascriptInterface(new GetSetupStatus(), "GetSetupStatus");
webView.loadUrl(startURL);
WebView.setWebContentsDebuggingEnabled(true);
I've been trying to sort this for weeks!
Thank you.