I have a project almost finished, which is an official application for the university. The only drawback is that the WebChromeClient does not allow it in the Fragment .
In Main
I have this code:
Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Problemas al descargar archivo. Intente más tarde. " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl(urlWeb);
//descargar archivo
webview.setDownloadListener(new DownloadListener()
{
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setMimeType(mimetype);
//------------------------COOKIE------------------------
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
//------------------------COOKIE------------------------
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Descargando Archivo...");
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));
final DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
new Thread("Descargar:") {
public void run() {
dm.enqueue(request);
}
}.start();
}
});
In the Manifest I have permission to write in memory, I just need that.