使用google doc给出错误,在android web视图中显示.DOCX文件

时间:2016-08-03 05:42:14

标签: android google-docs google-docs-api google-document-viewer

我必须在我的应用程序中显示远程URL链接的不同类型的文档。我在像这样的网页浏览中使用了Google文档

    private WebView mWvHome = (WebView) view.findViewById(R.id.wv_home);
    mWvHome.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {

                }
            });
    mWvHome.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return false;
                }
                @Override
                public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
                }
            });    
    mWvHome.getSettings().setJavaScriptEnabled(true); // enable javascript
    //Pasting a Fixed URL link to the file I am getting this Error.
    mWvHome.loadUrl("http://docs.google.com/gview?embedded=true&url=http://followitdc.cloudapp.net/FollowitMediaAPIv4/images/bb629006-d930-4fbe-b9ec-91895dc5133c.docx");

这是我的布局文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_normal"
        android:layout_alignParentTop="true"
        android:max="100"
        android:progress="0"
        android:progressDrawable="@drawable/custom_progressbar"
        android:visibility="visible" />

    <WebView
        android:id="@+id/wv_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/progress" />


    <RelativeLayout
        android:id="@+id/rl_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/progress"
        android:background="@color/white">

        <ProgressBar
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

    </RelativeLayout>

</RelativeLayout>

从谷歌文档加载获得此响应后

enter image description here

.docx格式的大小小于2MB的文件正在加载一些小于2MB的文件也没有加载。这是什么问题?是文件大小问题还是文件中的错误。因为文件在Google Doc在线编辑器和Microsoft Word中正常工作。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

经过一番努力,我已经到达this link,他们提到“Office Web Apps Viewer”这是一个文件扩展名的在线应用程序,如('.ppt''.pptx''。doc','。。docx ','。xls','。xlsx')您只需在webview中提供在线文档链接

http://view.officeapps.live.com/op/view.aspx?src=[OFFICE_FILE_URL]

谷歌在线文档视图应用程序无法正确处理DOC和DOCX文件,并提出上述错误。但在使用Office Live app进行办公文件扩展后,一切正常。

答案 1 :(得分:0)

正如WebView中所述,在使用loadDataWithBaseURL时,WebView可以访问本地设备文件(通过&#39;文件&#39;方案网址)

  

仅当baseUrl指定的方案不是&#39; http&#39;,&#39; https&#39;,&#39; ftp&#39;,&#39; ftps&#39;,&#39约&#39;或者&#39; javascript&#39;。

但据我所知,WebView目前不支持显示办公文档。如果您的目标只是将某些HTML显示为UI的一部分,那么这可能很好。

请在此SO帖子中查看解决方案 - Android how to open a .doc extention file以及GitHub中的这篇文章 - AndroidDocxToHtml,这可能有所帮助。

答案 2 :(得分:0)

使用此:

String fileUrl = "https://cs.wmich.edu/elise/courses/cs526/Android-tutorial.docx";               
WebView personalWebView = findViewById(R.id.personalWebView);
personalWebView.getSettings().setJavaScriptEnabled(true);
personalWebView.loadUrl("https://drive.google.com/gview?embedded=true&url=" + fileUrl);

或者这个:

String fileUrl = "https://cs.wmich.edu/elise/courses/cs526/Android-tutorial.docx";
String doc="<iframe src='http://docs.google.com/gview?embedded=true&url="+fileUrl+"' width='100%' height='100%' style='border: none;'></iframe>";
WebView personalWebView = findViewById(R.id.personalWebView);
personalWebView.getSettings().setJavaScriptEnabled(true);
personalWebView.loadData( doc , "text/html",  "UTF-8");

答案 3 :(得分:0)

只需添加

url=url.replaceAll(" ","%20");
String newUA= "Chrome/43.0.2357.65 ";
webView.getSettings().setUserAgentString(newUA);
webView.loadUrl("https://view.officeapps.live.com/op/view.aspx?src="+url);

此处的url是文件可用的变量。