Webview app: How to download a file inside webview app & external link should open outside the app

前端 未结 1 587
终归单人心
终归单人心 2021-01-25 19:17

I want to download specific file inside android webview app ( although they are from external link not from my webview app URL) and all external link should open outside webview

相关标签:
1条回答
  • 2021-01-25 19:58
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
            boolean value = true;
            String extension = MimeTypeMap.getFileExtensionFromUrl(url);
            if (extension != null) {
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                String mimeType = mime.getMimeTypeFromExtension(extension);
                if (mimeType != null) {
                    if (mimeType.toLowerCase().contains("pdf")
                            || extension.toLowerCase().contains("ppt")
                            || extension.toLowerCase().contains("doc")
                            || extension.toLowerCase().contains("rar")
                            || extension.toLowerCase().contains("rtf")
                            || extension.toLowerCase().contains("exe")
                            || extension.toLowerCase().contains("apk")
                            || extension.toLowerCase().contains("jpeg")
                            || extension.toLowerCase().contains("png")
                            || extension.toLowerCase().contains("xls")
                            || extension.toLowerCase().contains("zip")
                            || extension.toLowerCase().contains("jpg")) {
                        DownloadManager mdDownloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
                        DownloadManager.Request request = new DownloadManager.Request(
                                Uri.parse(url));
                        String name= URLUtil.guessFileName(url,null,MimeTypeMap.getFileExtensionFromUrl(url));
                        File destinationFile = new File(Environment.getExternalStorageDirectory(),name);
                        request.setDescription("Downloading...");
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        // request.setDestinationUri(Uri.fromFile(destinationFile));
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,name);
                        mdDownloadManager.enqueue(request);
                        //value = false;
                    }
                }
                if (value) {
                    view.loadUrl(url);
                }
    
                if (!url.contains("my site url")) { // Could be cleverer and use a regex
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }else{
                new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Exit!")
                .setMessage("Are you sure you want to close?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                        finish();
    
                    }
    
                })
                .setNegativeButton("No", null)
                .show();
                     }
                return false;
            }
            return false;
    
        } 
    

    Replace the positive and negative buttons code to do whatever you like. i hope this answers your use case.

    0 讨论(0)
提交回复
热议问题