Translate this page

Download Any Type of Files in Webview Android Studio Java

Download all types of files in android webview

The Android java code below can be used to download any type of files to your Android device’s storage with the file’s original name, be it an .mp3, mp4,jpg or pdf, the code will download any file that is downloadable

This code is for Android webview, just paste it below in the oncreate method of webview activity

Remember that you need set the write to external storage permission in the manifest.

and must request user permission for devices starting android Marshmallow.

 

Here is the android java code to download all types of files from Webview

 

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

 

Related Posts

Here is How to Get Host Domain from URL in Android Java

Sometimes, we might need extract domain name such as www.google.com from the www.google.com/search=searchQuery. In android java, the code below can do it. It will output only the…

Android SoftKeyBoard Enter Key Handling

How to Handle Android Keyboard’s Enter Key to Support your Own Function By default, android soft keyboard’s enter key doesn’t support your own function even if you…

Test Intent and URL Schemes of Android Webview

URL Schemes Tester This page contains different URL Schemes that can be used to test various URL Schemes, it was created to test android webview, but other…

Realme XT Specifications, Launch Date, Price and Details

Realme xt details, launch date and reviews The Realme XT is an android smartphone with a massive 64 Mega pixels camera and qualcomm snapdragon 712 processor. Brand…

How to Copy a Webview Link in Android Java

Here is how to Copy current web address url in Android Java With the simple java code below, you can copy a link of current loaded website…

How to get Android App Version Programmatically in Java Android Studio

Here is how to get android app current version code programmatically in java android Sometimes, you will need get the current version number of your android app…

Leave a Reply

Your email address will not be published. Required fields are marked *