-1

I have an Android app where I want to display files in pdf, excel, docx, txt formats from their respective Urls. I am able to display these files using external apps, but for this, the file is first downloaded and stored on the device and then displayed. I do not want the downloading, I tried using Google document viewer to display the files in a WebView, but since the documents are private, they wont display. I have tried something like below :-

    mDocumentUrl = mIntent.getStringExtra(Constant.DOCUMENT_URL);

    wvDocumentReader.getSettings().setJavaScriptEnabled(true);

    wvDocumentReader.setWebViewClient(new WebViewClient(){

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon)
                {
                    super.onPageStarted(view, url, favicon);
                    wvProgressBar.setVisibility(View.VISIBLE);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
                {
                    view.loadUrl(request.getUrl().toString());
                    return true;
                }

                @Override
                public void onPageFinished(WebView view, String url)
                {
                    super.onPageFinished(view, url);
                    wvProgressBar.setVisibility(View.GONE);
                }
            });
  wvDocumentReader.loadUrl("http://docs.google.com/gview?embedded=true&url="+ mDocumentUrl);

Can anyone suggest a library or a workaround to achieve this? Any help is appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user3034944
  • 1,541
  • 2
  • 17
  • 35
  • Of course you can't display private documents. This is not supposed to work and it's a good thing! – finki Nov 28 '18 at 07:19
  • 1
    @finki Ofcourse. We have an iOS app as well, where the document link opens up in a WebView, but before displaying it an login is asked only on a successful login, the document is displayed. Right now, Android WebView asks for the login and on successful login the document is downloaded to the device. I am trying to avoid the download – user3034944 Nov 28 '18 at 07:31
  • Possible duplicate of [How to open/display documents(.pdf, .doc) without external app?](https://stackoverflow.com/questions/14578530/how-to-open-display-documents-pdf-doc-without-external-app) – Mahendra Gohil Nov 28 '18 at 08:45
  • @MahendraGohil I have mentioned the same in my question that I have already tried the answer given in that question. But my documents are private and hence won't work. – user3034944 Nov 28 '18 at 11:18

1 Answers1

0

I think you should use custom library for getting that done .

See : https://github.com/googlesamples/android-PdfRendererBasic

There is also another way for displaying PDF without calling another application

This is a way for showing PDF in android app that is embed PDF document to android webview using support from http://docs.google.com/viewer

pseudo

String doc="<iframe src='http://docs.google.com/viewer?url=+location to your PDF File+' 
          width='100%' height='100%' 
          style='border: none;'></iframe>";

a sample is is shown below

 String doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' 
          width='100%' height='100%' 
          style='border: none;'></iframe>";

Code

WebView  wv = (WebView)findViewById(R.id.webView); 
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl(doc);
//wv.loadData( doc, "text/html",  "UTF-8");

and in manifest provide

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

OR

See this :https://stackoverflow.com/a/43292083/9976418

Mahendra Gohil
  • 380
  • 1
  • 3
  • 21