如何通过webview保存图片?
How to save image through webview?
假设我在 webview http://demo.io and this URL has a button of camera and when I click on that camera button then my URL became http://demo.io/captureImage. Then I have an issue when I check the url of webview then it shows only http://demo.io not showing http://demo.io/captureImage so how I can get this URL in Android. Because this URL has an image and I wanna save this image. When this URL (http://demo.io/captureImage 中使用 URL) 在 chrome 上运行并且我捕获图像然后捕获图像下载但是在 Android webview 图像之后捕获不下载。请给我建议合适的答案或代码
您可以向 webview 添加一个下载监听器并从 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(...)
假设我在 webview http://demo.io and this URL has a button of camera and when I click on that camera button then my URL became http://demo.io/captureImage. Then I have an issue when I check the url of webview then it shows only http://demo.io not showing http://demo.io/captureImage so how I can get this URL in Android. Because this URL has an image and I wanna save this image. When this URL (http://demo.io/captureImage 中使用 URL) 在 chrome 上运行并且我捕获图像然后捕获图像下载但是在 Android webview 图像之后捕获不下载。请给我建议合适的答案或代码
您可以向 webview 添加一个下载监听器并从 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(...)