Webview 应用程序:如何在 webview 应用程序中下载文件 &外部链接应在应用程序外打开 [英] Webview app: How to download a file inside webview app & external link should open outside the app

查看:54
本文介绍了Webview 应用程序:如何在 webview 应用程序中下载文件 &外部链接应在应用程序外打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 android webview 应用程序中下载特定文件(虽然它们来自外部链接而不是来自我的 webview 应用程序 URL)并且所有外部链接都应该在 webview 应用程序外部打开意味着手机浏览器.

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);
        }
    }

当我单击包含上述扩展名的任何链接时,文件会在 webview 应用程序中开始下载(没关系),但同时该链接会自动打开到我的移动浏览器中.我知道这是因为这段代码.

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;

现在,

首先解决方案我想知道如何在 webview 应用程序中下载包含上述扩展名的文件,并且所有不包含上述扩展名的链接都应该在我的 webview 应用程序之外打开,这意味着移动浏览器.

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.

推荐答案

 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.

这篇关于Webview 应用程序:如何在 webview 应用程序中下载文件 &外部链接应在应用程序外打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆