【问题标题】:How can I open pdf in my webview using google Docs? (Suddenly it's not work)如何使用 google Docs 在我的 webview 中打开 pdf? (突然不行了)
【发布时间】:2014-11-24 09:13:00
【问题描述】:

年龄不长,我可以使用下面的代码在我的 webview 中打开 pdf。

view.loadUrl("https://docs.google.com/gview?embedded=true&url=" + str);

但是突然不行了。

我不知道原因。

如何在我的网页视图中打开 pdf??

【问题讨论】:

    标签: android pdf webview


    【解决方案1】:

    创建一个下载器类

    public class Downloader {
    
        public static void DownloadFile(String fileURL, File directory) {
            try {
    
                FileOutputStream f = new FileOutputStream(directory);
                URL u = new URL(fileURL);
                HttpURLConnection c = (HttpURLConnection) u.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();
    
                InputStream in = c.getInputStream();
    
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = in.read(buffer)) > 0) {
                    f.write(buffer, 0, len1);
                }
                f.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    

    在你的活动中setContentView(R.layout.main);write 这些行

    String extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();
            File folder = new File(extStorageDirectory, "pdf");
            folder.mkdir();
            File file = new File(folder, "Read.pdf");
            try {
                file.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            Downloader.DownloadFile("URL", file);
    
            showPdf();
    

    写这个方法

     public void showPdf()
            {
                File file = new File(Environment.getExternalStorageDirectory()+"/Mypdf/Read.pdf");
                PackageManager packageManager = getPackageManager();
                Intent testIntent = new Intent(Intent.ACTION_VIEW);
                testIntent.setType("application/pdf");
                List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                Uri uri = Uri.fromFile(file);
                intent.setDataAndType(uri, "application/pdf");
                startActivity(intent);
            }
    

    在您的AndroidManifest.xml 中添加这些权限

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    【讨论】:

      【解决方案2】:

      我认为这个问题是重复的,我得到了解决方案

      1) 类型:

       webView.loadUrl("https://docs.google.com/gview?url="+pdfUrl.get(position).url);
      

      2) 别忘了设置 webview 客户端

      webView.setWebViewClient(new WebViewClient() {
              ProgressDialog progressDialog;
      
              //If you will not use this method url links are opeen in new brower not in webview
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  view.loadUrl(url);
                  return true;
              }
      
              @Override
              public void onPageStarted(WebView view, String url, Bitmap favicon) {
                  super.onPageStarted(view, url, favicon);
                  if (progressDialog == null) {
                      // in standard case YourActivity.this
                      progressDialog = new ProgressDialog(getActivity());
                      progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                      progressDialog.setMessage("Loading...");
                      progressDialog.show();
                  }
              }
      
              public void onPageFinished(WebView view, String url) {
                  if (progressDialog != null && progressDialog.isShowing()) {
                      progressDialog.dismiss();
                      progressDialog = null;
                  }
      
              }
      
          });
      

      【讨论】:

        猜你喜欢
        • 2013-10-07
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-09
        相关资源
        最近更新 更多