Download Images Using Android Webview

Download Images using android webview

Try adding download listener -

mWebView.setDownloadListener(new DownloadListener() {

public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {

Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();

request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, //Download folder
"download"); //Name of file

DownloadManager dm = (DownloadManager) getSystemService(
DOWNLOAD_SERVICE);

dm.enqueue(request);

}
});

How to download an image from a WebView

You need to setDownloadListener method like this Reference Answer

myWebView .setDownloadListener(new DownloadListener() {

public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {

Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();

request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, //Download folder
"download"); //Name of file

DownloadManager dm = (DownloadManager) getSystemService(
DOWNLOAD_SERVICE);

dm.enqueue(request);

}
});

How to download image from Webview to custom/private folder instead of Download folder in Android

try this

mywebView.setDownloadListener(new DownloadListener() {

public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();

request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

File folder = new File("/storage/emulated/0/Folder_Name");
if (!folder.exists()) {
folder.mkdirs();
System.out.println("Directory created");
} else {
System.out.println("Directory is not created");
}
request.setDestinationInExternalPublicDir("Folder_Name","Wallpaper.jpg");

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

dm.enqueue(request);

}
});

How to save image through webview?

You can add a download listner to webivew and download the image from the url as shown below

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();
}

Downloading images with webview

This solved my problem

`          `@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
boolean shouldOverride = false;
// We only want to handle requests for image files, everything else the webview
// can handle normally
if (url.endsWith(".jpg")) {
shouldOverride = true;
Uri source = Uri.parse(url);
// Make a new request pointing to the mp3 url
DownloadManager.Request request = new DownloadManager.Request(source);
// Use the same file name for the destination
File destinationFile = new File (destinationDir, source.getLastPathSegment());
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
manager.enqueue(request);
}
return shouldOverride;
}``

make sure to add permissions for download manager, SD read, SD write!



Related Topics



Leave a reply



Submit