假设我在Webview http://demo.io中使用一个URL,并且此URL有一个摄像头按钮,当我单击该摄像头按钮时,我的URL变为http://demo.io/captureImage。然后,当我检查webview的URL时出现问题,它仅显示http://demo.io,不显示http://demo.io/captureImage,因此如何在Android中获取此URL。因为此URL包含图像,所以我想保存该图像。当此URL(http://demo.io/captureImage)在chrome上运行时,我捕获了图像,然后捕获了已下载的图像,但捕获后未下载,但捕获了Android webview图像。请建议我适当的答案或代码
您可以将下载列表添加到webivew并从URL下载图像,如下所示
webview.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.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();
}
如果我没问题,
首先,您必须使用新的URL重置WebView
。请尝试以下操作,
cameraButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
UrActivity.this.mWebView.loadUrl("http://demo.io/captureImage");
}});
第二个是下载图像,您可以按照@Hasif Seyd的建议使用setDownloadListener(...)