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

最后都变了- 提交于 2019-12-31 04:10: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 app means phone browser.

I used code below to do that.

 wv.setWebViewClient(new WebViewClient(){


        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;
                }
                return false;
            }
            return false;

        }   


        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)  {
            wv.loadUrl(mypage_error);
        }
    }

When i click any link containing above extension, the file starting download inside the webview app (It's OK) but at the same time that link automatically opening into my mobile browser. I know this is happening because of this code.

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;
                }
                return false;

Now,

Firstly Solution I want know how can I download files containing above extension inside webview app and all link that don't contain above extension should open outside my webview app that means mobile browser.

Secondly Solution If it is not possible then tell me, when I click the link how to show alert/popup (browser choosing option) every time. As i mentioned instead of showing browser choosing alert links are opening automatically to my phone browser.


回答1:


 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.



来源:https://stackoverflow.com/questions/45730791/webview-app-how-to-download-a-file-inside-webview-app-external-link-should-op

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!