【问题标题】:I'm getting no preview available message when try to Open online PDF file in webview尝试在 webview 中打开在线 PDF 文件时,我没有收到可用的预览消息
【发布时间】:2019-03-17 13:46:37
【问题描述】:

我的代码是这样的 WebView webview = new WebView(getActivity());

    webview.getSettings().setJavaScriptEnabled(true);

   final ProgressDialog progDailog = ProgressDialog.show(getActivity(), "Loading", "Please wait...", true);
    progDailog.setCancelable(false);


    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setPluginState(WebSettings.PluginState.ON);

// webview.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); //以下几行显示加载器,直到下载 pdf 文件以供查看。 webview.setWebViewClient(新的 WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            progDailog.show();
            view.loadUrl(url);

            return true;
        }

        @Override
        public void onPageFinished(WebView view, final String url)
        {
            progDailog.dismiss();
        }
    });
    try {
        String urlEncoded = URLEncoder.encode(pdf, "UTF-8");
        pdf = "http://docs.google.com/viewer?url=" + urlEncoded;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
   String pdf = "http://192.168.2.154:8080/streamline/res/INV-000015.pdf";
    webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

【问题讨论】:

    标签: android pdf webview


    【解决方案1】:

    192.168.2.154 是您本地网络上的一台机器。 docs.google.com 无法访问它。 docs.google.com 只能访问公共 IP 地址上的公共 URL。

    也许你可以考虑other options for viewing PDFs

    【讨论】:

    • 这是正确答案。您无法访问 pdf,因为您使用 pdf 的本地 IP。尝试将其部署到公共域,它应该可以正常工作。您可能会考虑其他选项,例如 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdfLink)); startActivity(browserIntent); 但请注意,此解决方案可能无法在较低版本的 android bellow 7 上正常工作。我在使用三星设备时遇到了一些问题,尤其是使用三星互联网等默认浏览器。跨度>
    【解决方案2】:

    这对我有用...我所做的是在将其与 url 连接之前将 url 解析为 Uri 并且一切正常。以下是我的代码:

    // sets visibility to visible
    progressBar.setVisibility(View.VISIBLE);
    
    
    // displaying document in webview
    String url = Uri.encode(fileUrl);
    
    
    document_viewer.getSettings().setJavaScriptEnabled(true);
    document_viewer.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    document_viewer.getSettings().setBuiltInZoomControls(true);
    document_viewer.getSettings().setUseWideViewPort(true);
    //document_viewer.getSettings().setPluginState(WebSettings.PluginState.ON);
    // loads documentUrl into webView
    document_viewer.loadUrl("http://docs.google.com/gview?embedded=true&url="+url);
    
    document_viewer.setWebViewClient(new WebViewClient() {
    
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
            super.onPageStarted(view, url, favicon);
            // sets visibility to visible
            progressBar.setVisibility(View.VISIBLE);
    
        }
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // do your stuff here
            // sets visibility of progressBar to gone
            progressBar.setVisibility(View.GONE);
    
            // sets visibility of webView to visible
            document_viewer.setVisibility(View.VISIBLE);
    
            // setting the details of document on text Views
            title.setText(" Title : " + documentTitle);
            tag.setText(" Tag : " + documentTag);
            type.setText(" Type : " + documentType);
            comment.setText(" Comment : " + documentComment);
            distributee.setText(" Distributee : "  + documentDistributee);
        }
    
        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            view.loadUrl("about:blank");
            Toast.makeText(getApplicationContext(), getResources().getString(R.string.error_occurred), Toast.LENGTH_LONG).show();
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    
    });
    

    希望对您有所帮助。谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2018-06-01
      • 2020-04-02
      • 2018-07-25
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多