Webview应用程序:如何在webview应用程序中下载文件&外部链接应在应用

时间:2017-08-17 09:02:15

标签: android webview android-download-manager

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

我使用下面的代码来做到这一点。

 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应用程序内下载(它没问题),但同时该链接会自动打开我的移动浏览器。我知道这是因为这段代码而发生的。

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应用程序之外打开,这意味着移动浏览器。< / p>

第二个解决方案 如果不可能那么告诉我,当我点击链接时每次都显示警报/弹出窗口(浏览器选择选项)。正如我所提到的,而不是显示浏览器选择警报链接会自动打开到我的手机浏览器。

1 个答案:

答案 0 :(得分: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;

    } 

替换正负按钮代码以执行您喜欢的任何操作。 我希望这能回答你的用例。