如何在Android中的Webview中打开Excel,.doc文件?

Ash*_*agi 5 android

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

Md *_*fur 21

是的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);

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.它的工作就像一个魅力.我可以加载我的本地文档吗?意味着我已经在SDCard中下载了.doc文件,现在我想用它来显示它们.那有可能吗? (3认同)

And*_*ree 5

如果您想从内部存储打开文档文件,例如 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");
Run Code Online (Sandbox Code Playgroud)

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


添加<provider>到AndroidManifest.xml 文件中。

<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>
Run Code Online (Sandbox Code Playgroud)

添加res/xml/file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <root-path name="root" path="." />
</paths>
Run Code Online (Sandbox Code Playgroud)

最后在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();
}
Run Code Online (Sandbox Code Playgroud)