Download file inside WebView

sapan
Download file from webView

The below code demonstrate how we can download file from the webview.

permissions required

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

code

public class MainActivity extends AppCompatActivity {  
  
    Context mContext;  
    @Override  
  protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mContext=this;  
        //find the webView  
  WebView myWebView = (WebView) findViewById(R.id.myweb);  
  
        //Load url in webview  
 //myWebView.setWebChromeClient(new WebChromeClient()); //--> issue with android pie and above. opens default browser  
  myWebView.setWebViewClient(new WebViewClient());  
        myWebView.getSettings().setJavaScriptEnabled(true);  
  
        myWebView.setDownloadListener(new DownloadListener() {  
            @Override  
  public void onDownloadStart(final String url, final String userAgent, String contentDisposition, String mimetype, long contentLength)  
            {  
                //Checking runtime permission for devices above Marshmallow.  
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
                    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)  
                            == PackageManager.PERMISSION_GRANTED) {  
                        Log.v("SKDINFO","Permission is granted");  
                        downloadDialog(url,userAgent,contentDisposition,mimetype);  
  
                    } else {  
  
                        Log.v("SKDINFO","Permission is revoked");  
                        //requesting permissions.  
  ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);  
  
                    }  
                }  
                else {  
                    //Code for devices below API 23 or Marshmallow  
  Log.v("SKDINFO","Permission is granted");  
                    downloadDialog(url,userAgent,contentDisposition,mimetype);  
  
                }  
            }  
        });  
        myWebView.loadUrl("http://www.androidlearner.com");  
  
  
    }  
  
    public void downloadDialog(final String url,final String userAgent,String contentDisposition,String mimetype)  
    {  
        //getting filename from url.  
  final String filename = URLUtil.guessFileName(url,contentDisposition,mimetype);  
        //alertdialog  
  AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);  
        //title of alertdialog  
  builder.setTitle("Download");  
        //message of alertdialog  
  builder.setMessage("Do you want to save " +filename);  
        //if Yes button clicks.  
  builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()  
        {  
            @Override  
  public void onClick(DialogInterface dialog, int which)  
            {  
                //DownloadManager.Request created with url.  
  DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));  
                //cookie  
  String cookie= CookieManager.getInstance().getCookie(url);  
                //Add cookie and User-Agent to request  
  request.addRequestHeader("Cookie",cookie);  
                request.addRequestHeader("User-Agent",userAgent);  
                //file scanned by MediaScannar  
  request.allowScanningByMediaScanner();  
                //Download is visible and its progress, after completion too.  
  request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  
                //DownloadManager created  
  DownloadManager downloadManager=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);  
                //Saving files in Download folder  
  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);  
                //download enqued  
  downloadManager.enqueue(request);  
            }  
        });  
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
            @Override  
  public void onClick(DialogInterface dialog, int which)  
            {  
                //cancel the dialog if Cancel clicks  
  dialog.cancel();  
            }  
  
        });  
        //alertdialog shows.  
  builder.create().show();  
  
    }    
} 

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !