Android webview无法下载文件

时间:2014-06-23 07:29:42

标签: android android-webview

我正在使用一个HTML应用程序。我现在浏览webview,该网站将允许下载PDF文件。
现在的问题是当在设备浏览器中加载该URL时它将自动下载PDF文件但在webview中无法下载我在下面的代码中使用过的文件。

webView.setWebViewClient(new Callback());
webView.setWebChromeClient(new webChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSavePassword(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
webView.getSettings().setSupportMultipleWindows(false);
webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) {
          AppLog.logString(TAG+"setDownloadListener");
          AppLog.logString(TAG+"url: "+url);
          //Intent intent = new Intent(Intent.ACTION_VIEW);
          //intent.setData(Uri.parse(url));
          //startActivity(intent);
        }
    });
    webView.loadUrl(url);

private class Callback extends WebViewClient{  
    String TAG="ClBk: ";
    @SuppressWarnings("unused")
    @SuppressLint("DefaultLocale")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        AppLog.logString(TAG+"shouldOverrideUrlLoading");
        AppLog.logString(TAG+"url: "+url);

        if(!isInternetOncheck()){
            intenetDialog();
        }else{

            boolean value = true;  
            String extension = MimeTypeMap.getFileExtensionFromUrl(url);  
            AppLog.logString(TAG+"extension: "+extension);
            if (extension != null) {  
                 MimeTypeMap mime = MimeTypeMap.getSingleton();  
                 String mimeType = mime.getMimeTypeFromExtension(extension);
                 AppLog.logString(TAG+"mimeType: "+mimeType);
                 if (mimeType != null) {  
                      if (mimeType.contains("application/zip")
                                ||mimeType.toLowerCase().contains("video")  
                                || extension.toLowerCase().contains("mov")  
                                || extension.toLowerCase().contains("mp3")) {
                           AppLog.logString(TAG+"If MimeType for dowloading");
                           DownloadManager mdDownloadManager = (DownloadManager) MainActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);  
                           DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));  
                           File destinationFile = new File(Environment.getExternalStorageDirectory(),getFileName(url,extension));  
                           request.setDescription("Downloading via Your app name..");  
                           request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  
                           request.setDestinationUri(Uri.fromFile(destinationFile));  
                           mdDownloadManager.enqueue(request);  
                           value = false;  
                      }  
                 }  
            }  
            if(url.contains(".pdf")){
            AppLog.logString(TAG+"pdf");
            view.setDownloadListener(new DownloadListener() {
                @SuppressLint("NewApi")
                public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype, long contentLength) {
                    AppLog.logString(TAG+"download pdf");
                    startDownloadPDF(url);
                }
            });
            }else if(url.contains(".rar")){
                AppLog.logString(TAG+"rar");
                view.setDownloadListener(new DownloadListener() {
                    @SuppressLint("NewApi")
                    public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype, long contentLength) {
                        AppLog.logString(TAG+"download rar");
                        startDownloadZBS(url);
                    }
                });
            }else{
                AppLog.logString(TAG+"else: "+url);
            }
            progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;
        }
        return true;
    }
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        AppLog.logString(TAG+"onPageStarted");
        //AppLog.logString(TAG+"url: "+url);
        progressBar.setVisibility(View.VISIBLE);
        if(!isInternetOncheck()){
            intenetDialog();
        }
        super.onPageStarted(view, url, favicon);
    }   
    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        AppLog.logString(TAG+"onPageFinished");
        //AppLog.logString(TAG+"url: "+url);
        progressBar.setVisibility(View.INVISIBLE);
        webView.setVisibility(View.VISIBLE);
        ((RelativeLayout)findViewById(R.id.layoutSplace)).setVisibility(View.GONE);
        super.onPageFinished(view, url);
    }   
 }

在浏览器中,当url下载pdf文件时,它在这个url中下载(http:/\ t = rptTrialBalance / ExporttoPDF?dtSession1 = PDFReport& FileName = TrialBa lance& download = true)在浏览器中下载但是webview无法下载此

我已经检查过这个网址,但它无效How to use a download Manager in a webview to download pdf/ppt/doc files to sdcard

3 个答案:

答案 0 :(得分:0)

我认为您需要使用Chrome CLient。

//webExternalLinks is your webview.

    webExternalLinks.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                    if(seekBar==null){
                        seekBar = IjoomerUtilities
                                .getLoadingDialog(getString(R.string.dialog_loading_please_wait));
                    }
                    seekBar.setProgress(progress);
                    if(progress==100){
                        seekBar=null;
                    }
                }
            });

            webExternalLinks.setWebViewClient(new WebViewClient());
            webExternalLinks.getSettings().setJavaScriptEnabled(true);
            webExternalLinks.getSettings().setPluginState(PluginState.ON);
            webExternalLinks.getSettings().setSupportZoom(true);
            webExternalLinks.getSettings().setBuiltInZoomControls(true);

            if (link != null && link.length() > 0) {
                if (!link.startsWith("http://") && !link.startsWith("https://")) {
                    link = "http://" + link;
                }
                webExternalLinks.loadUrl(link);
            }

答案 1 :(得分:0)

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

试试这个: 首先,plz打印您在URI中传递的URL。并在Browser.is中打开该URL是否打开? 如果它没有打开browswer然后问题得到网址。

答案 2 :(得分:0)

从breif看,你没有确定pdf下载是你的条件 url.contains( “PDF”) 但是您的URL不包含此字符串。 你应该以某种方式获取响应头内容类型中返回的内容类型。 希望有所帮助。 Nitzan