当用户单击WebView中的链接时,我需要下载文件。当我在chrome应用程序中执行相同操作时,该操作正常工作,但在Webview中不起作用。我已经提到了多个问题,并尝试实施提供的解决方案,但这没有用。
提到了Stackoverflow问题并尝试了解决方案。
Android Webview not triggering a href download file
How to download files from webview Android?
我使用的WebView代码,
wv_main.getSettings().setJavaScriptEnabled(true);
wv_main.getSettings().setDomStorageEnabled(true);
wv_main.getSettings().setAllowFileAccess(true);
wv_main.getSettings().setAllowFileAccessFromFileURLs(true);
wv_main.getSettings().setAllowUniversalAccessFromFileURLs(true);
wv_main.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv_main.getSettings().setBlockNetworkLoads(false);
wv_main.getSettings().setAllowContentAccess(true);
wv_main.getSettings().setPluginState(WebSettings.PluginState.ON);
wv_main.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Download file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is 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();
}
});
wv_main.setWebViewClient(new WebViewClient() {
private int webViewPreviousState;
private final int PAGE_STARTED = 0x1;
private final int PAGE_REDIRECTED = 0x2;
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// Get cert from SslError
SslCertificate sslCertificate = error.getCertificate();
Certificate cert = getX509Certificate(sslCertificate);
if (cert != null && certificate != null) {
try {
// Reference: https://developer.android.com/reference/java/security/cert/Certificate.html#verify(java.security.PublicKey)
cert.verify(certificate.getPublicKey()); // Verify here...
handler.proceed();
} catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException e) {
super.onReceivedSslError(view, handler, error);
e.printStackTrace();
}
} else {
super.onReceivedSslError(view, handler, error);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
webViewPreviousState = PAGE_REDIRECTED;
wv_main.loadUrl(urlNewString);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewPreviousState = PAGE_STARTED;
Log.e("Start Loading", "Start Loading.........");
/*if (dialog == null || !dialog.isShowing())
dialog = ProgressDialog.show(MainActivity.this, "", "Loading", true, true);*/
}
@Override
public void onPageFinished(WebView view, String url) {
if (webViewPreviousState == PAGE_STARTED) {
//dialog.dismiss();
Log.e("Loading Done", "Loading Done.........");
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
super.onReceivedError(view, request, error);
Toast.makeText(SecretLinkBrowserActivity.this, "Error:" + error.toString(), Toast.LENGTH_SHORT).show();
}
});
wv_main.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
request.grant(request.getResources());
}
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d("LogTag", message);
result.confirm();
return true;
}
});
在调试时,我注意到单击链接时shouldOverrideUrlLoading()
没有被调用。网站进行下载的方式是,他们使用了doPostBack()
来动态生成网址并下载文件。
答案 0 :(得分:0)
您可以启动一个新的意图,并允许用户选择另一个应用程序进行下载,如下所示:
wv_main.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);
}
});
但是我想您想使用下载管理器,您缺少setDestinationUri方法调用,应该是这样的
DownloadManager.Request request=new DownloadManager.Request(Uri.parse(url))
.setTitle("Dummy File")// Title of the Download Notification
.setDescription("Downloading")// Description of the Download Notification
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
.setDestinationUri(Uri.fromFile(file))// Uri of the destination file
.setRequiresCharging(false)// Set if charging is required to begin the download
.setAllowedOverMetered(true)// Set if download is allowed on Mobile network
.setAllowedOverRoaming(true);// Set if download is allowed on roaming network