1

I have to show different type of documents from a remote URL link in my application. I have use Google Doc in a webview like this

    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");

Here is my Layout file

 <?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>

After loading getting this response from google doc.

enter image description here

File of size less than 2MB in .docx format are loading some file less than 2MB are also not loading. What it the issue? Is it file size issue or Error in file. Because file is working properly in Google Doc online Editor and also on Microsoft Word. Any help will be appreciated.

jawad bin zafar
  • 163
  • 1
  • 14
  • http://docs.google.com/gview?embedded=true&url this will not work in some of android devices. – MathankumarK Aug 03 '16 at 05:50
  • what should I do to fix it @MathaN – jawad bin zafar Aug 03 '16 at 05:55
  • If you have backend team tell them to give a link to read a document files. It will be better. I have done the same for my application. – MathankumarK Aug 03 '16 at 06:05
  • They are actually giving a read link. I converting this read link to show in Google Doc view so that I don't have to render the file by my own just using the already build components. How you using the read link? like using any library or what to display the documents. I have to read PDF, DOC, DOCX and other file type supported by Google Doc. Any help @MathaN – jawad bin zafar Aug 03 '16 at 06:24
  • I have used this link docs.google.com/gview?embedded=true&url, But it doesn't work. So backend team have created a web app. I have used that app in my WebView to read documents link. It's same as Google doc reader – MathankumarK Aug 03 '16 at 06:27
  • Is app is a web base document view which you provide a link and it display the content of the file @MathaN – jawad bin zafar Aug 03 '16 at 06:52
  • String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf"; webview.loadUrl("http://domain.com/documentsviewer?url=" + pdf); – MathankumarK Aug 03 '16 at 07:00
  • This will only work for pdf and not for docx file. @MathaN – jawad bin zafar Aug 03 '16 at 07:27

4 Answers4

2

After some struggle i have reached to this link in which they have mention "Office Web Apps Viewer" an online app for file extension like ('.ppt' '.pptx' '.doc', '.docx', '.xls', '.xlsx') you just need to provide the online document link in your webview

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

Google online doc view app does't handle the DOC and DOCX file properly for me and giving the above error I have mention. But after using Office live app for office file extension everything work just fine.

jawad bin zafar
  • 163
  • 1
  • 14
0

As discussed in WebView, in using loadDataWithBaseURL, WebView can access local device files (via 'file' scheme URLs)

only if baseUrl specifies a scheme other than 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'.

However, as far as I know, WebView currently does not support displaying of office documents. If your goal is only to display some HTML as a part of your UI, this is probably fine.

Please check solution in this SO post - Android how to open a .doc extention file and this post in GitHub - AndroidDocxToHtml which might help.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22
0

Use this:

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

or this:

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");
Princewill Iroka
  • 536
  • 1
  • 7
  • 17
0

Simply add

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

Here url is the variable where your file is available.

Aniket
  • 391
  • 5
  • 13