【问题标题】:How to open Excel, .doc files in Webview in Android?如何在 Android 的 Webview 中打开 Excel、.doc 文件?
【发布时间】:2012-10-09 10:07:27
【问题描述】:

如何在 Android webview 中打开 Excel 和 .doc 文件。 google doc可以支持吗?

【问题讨论】:

  • 我想尝试在我的 android 项目的 webview 中打开 Excel 和 .doc 文件,但它没有打开,请任何伙伴帮助我,提前谢谢
  • 你能分享你所做的代码吗?这样我就可以看到代码并轻松检查它了..
  • Open PDF in a WebView的可能重复
  • 检查一下,这是最好的解决方案: stackoverflow.com/a/12797706

标签: android


【解决方案1】:

是的 Google doc 支持您显示 doc 或 excel 、 pdf、 txt 或其他格式。

WebView urlWebView = (WebView)findViewById(R.id.containWebView);
urlWebView.setWebViewClient(new AppWebViewClients());
urlWebView.getSettings().setJavaScriptEnabled(true);
urlWebView.getSettings().setUseWideViewPort(true);
urlWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="
                + "YOUR_DOC_URL_HERE"); 

public class AppWebViewClients extends WebViewClient {



    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

    }
}

【讨论】:

  • 谢谢。它的工作就像一个魅力。我可以在其中加载我的本地文档吗?意味着我已经在我的 SDCard 中下载了 .doc 文件,现在我想用它来展示它们。那么有可能吗?
  • 如果我的文档在 assets 文件夹中,如何加载它?
  • 是否加载.pptx 文件类型?
【解决方案2】:

如果您想从内部存储中打开文档文件,例如 file:///data/user/0/com.sample.example/files/documents/sample.docx,那么您 不能用

urlWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="+"YOUR_DOC_URL_HERE");

您必须从外部应用程序(如 google docs、MS Word 等)打开 docx 文件,为此您可以使用 FileProvider


在 AndroidManifest.xml 文件中添加<provider>

<application>
 <provider
   android:name="androidx.core.content.FileProvider"
   android:authorities="com.sample.example.provider" // you have to provide your package name here add add .provider after your package name
   android:exported="false"
   android:grantUriPermissions="true">
   <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/file_paths" />
 </provider>
</application>

添加 res/xml/file_paths.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <root-path name="root" path="." />
</paths>

最后在MainActivity.java文件中添加打开docx文件的代码

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();

Uri docUri = FileProvider.getUriForFile(getApplicationContext(), 
  "com.sample.example.provider", 
  new File("/data/user/0/com.sample.example/files/documents/sample.docx")); // same as defined in Manifest file in android:authorities="com.sample.example.provider"
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(docUri, "application/msword");
try{
   intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
   Intent chooser = Intent.createChooser(intent,"Open With..");
   startActivity(chooser);
} catch (ActivityNotFoundException e) {
   //user does not have a pdf viewer installed
   Log.d(LOG_TAG, "shouldOverrideUrlLoading: " + e.getLocalizedMessage());
   Toast.makeText(MainActivity.this, "No application to open file", Toast.LENGTH_SHORT).show();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2010-12-06
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2015-02-02
    相关资源
    最近更新 更多