Download Any Type of Files in WebView Android Studio

How to Download Any Type of Files in WebView Android Studio?

If your website have download option you really need to enable it from your Android WebView app. 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.

Download Manager
Download Any Type of Files in WebView Android Studio?


myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Toast.makeText(getApplicationContext(),"Downloading File",

And also Remember that you need set the write to external storage permission in the manifest. If not above code will not work.

in AndroidMainfest.xml

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

And also we must request user permission for devices. In next Article We will tell you How to request user permission for devices in Android Studio?

Read also:

Danushka Sanjeewa

Danushka Sanjeewa is a Software engineer with more than 5 years of experience in Android App Development, Web Development, Marketing, Graphic design, Video editing, and WordPress. And also Writer/Founder of

Leave a Comment