Breaking News

How to create a Download listener with Android WebView and Download Files


In this post, we will show you how to download files with the use of Download listener with WebView in your Android App.

In this, we use inbuilt download manager to download files from WebView.


For that, we use download listener with Webview in OnCreate of Main Activity.java


MainActivity.java

 mywebview.setDownloadListener(new DownloadListener() {  
       @Override  
       public void onDownloadStart(String url, String useragent, String contentdisposition, String mimetype, long contentlength) {  
           if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)  
           {  
             if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)  
             {  
               DowloadDialog(url,useragent,contentdisposition,mimetype);  
             }  
             else  
             {  
               ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);  
             }  
           }  
           else  
           {  
             DowloadDialog(url,useragent,contentdisposition,mimetype);  
           }  
       }  
     });  
   }  

Here, we use Download listener with WebView and when download starts we check storage permission and if permission is granted then we call our DownloadDialog method which we will create further to show dialog box when we download something.

Then we create a Download Dialog method after the OnCreate method of MainActivity. In the Download Dialog method, we get filename and call the Download Manager to download files.

MainActivity.java Dialog box method
  public void DowloadDialog(final String url, final String UserAgent, String contentdisposition, String mimetype)  
   {  
     final String filename = URLUtil.guessFileName(url,contentdisposition,mimetype);  
     final AlertDialog.Builder builder = new AlertDialog.Builder(this);  
     builder.setTitle("Downloading...")  
         .setMessage("Do you want to Download "+ ' '+" "+filename+" "+' ')  
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialogInterface, int i) {  
             DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));  
             String cookie = CookieManager.getInstance().getCookie(url);  
             request.addRequestHeader("Cookie",cookie);  
             request.addRequestHeader("User-Agent",UserAgent);  
             request.allowScanningByMediaScanner();  
             request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  
             DownloadManager manager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);  
             request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);  
             manager.enqueue(request);  
           }  
         })  
         .setNegativeButton("No", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialogInterface, int i) {  
             dialogInterface.cancel();  
           }  
         })  
         .show();  
   }  

Here, we create a dialog box that is shown when our Download is starting and also we get the filename, user-agent and mimetype in this. We call Download manager and we set download directory in it.

Don't forget to write storage permission in your AndroidManifest

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


Next, we create a Context menu when we long click on Webview link or Image. 

Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments