How to download files in webView?

前端 未结 1 1158
小蘑菇
小蘑菇 2021-02-11 08:25

There are two ways to download files in a webView-

1) download via webview

// download manager
webView.setDownloadListener(new DownloadL         


        
相关标签:
1条回答
  • 2021-02-11 09:08

    You can use this snippet

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setMessage("How you want to download this file?")
                .setCancelable(false)
                .setPositiveButton("Use webView",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int id){
    
                        //your code here for download manager
                        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);
                                String cookies= CookieManager.getInstance().getCookie(url);
                                request.addRequestHeader("cookie",cookies);
                                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();} });
                        dismiss();}})
                .setNegativeButton("3rd Party App",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int id){
    
                        // download via...
                        webView.setDownloadListener(new DownloadListener(){
    
                            public void onDownloadStart(String url,String userAgent,
                                                        String contentDisposition,String mimetype,
                                                        long contentLength){
                                Intent i=new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(url));
                                startActivity(i);
                                Toast.makeText(getApplicationContext(),"don't choose our app as it can't handle download intents, i have posted a question on stackoverflow though.",Toast.LENGTH_SHORT).show(); }});dialog.cancel();} });
        AlertDialog alert=builder.create();
        alert.show();
    

    This is snippet i have added that will show you two options like you want just put your logic in button's on click method.

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("How you want to download?")
       .setCancelable(false)
       .setPositiveButton("By web", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //use logic of downloading with web view here
                dialog.cancel();
           }
       })
       .setNegativeButton("Use third party tool", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //use logic of third party tool here
                dialog.cancel();
           }
       });
    AlertDialog alert = builder.create();
    alert.show();
    
    0 讨论(0)
提交回复
热议问题