Shihab Uddin
Shihab Uddin

Reputation: 6931

show pdf in webview not works

I am showing a pdf with Google docs in webview. I am using url from Google Drive. I tried so many formats from Google Drive, But have no luck. My Url link as below

https://drive.google.com/drive/u/0/folders/0Bxd-5Rffh6BufmNiYS16NFZKN2JmYzd3TjgtWmxLeEZ2SnlMa2l2RDhmYlNyTDgtUXZOdnM

My Activity class as foloows"

public class FormWebViewActivity extends CustomWindow {

int value;
static String POSITION_ID = "position";
// WebView webView;

ProgressBar progressBar_webview;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {

        WebView webView = new WebView(FormWebViewActivity.this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setPluginState(PluginState.ON);

        String pdfURL = "https://drive.google.com/drive/u/0/folders/0Bxd-5Rffh6BufmNiYS16NFZKN2JmYzd3TjgtWmxLeEZ2SnlMa2l2RDhmYlNyTDgtUXZOdnM";
        // https://drive.google.com/file/d/0Bxd-5Rffh6BuSlhnTHJsMW5Hclk/view

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {

                super.onPageFinished(view, url);
                // progressBar_webview.setVisibility(View.GONE);
            }
            @Override
            public void onPageStarted(WebView view, String url,
                    Bitmap favicon) {

                super.onPageStarted(view, url, favicon);
                // progressBar_webview.setVisibility(View.VISIBLE);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // view.loadUrl(url);
                return false;
            }

        });

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

    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    // WebView webView = new WebView(FormWebViewActivity.this);
}

}

But it shows only HTML code, not the actual PDF view. Am i putting wrong Google Drive URL? Or It's not possible to show from Google Drive.

Any free web hosting for PDF file will also be appreciated.

Upvotes: 2

Views: 2101

Answers (2)

King of Masses
King of Masses

Reputation: 18775

In addition to the @Derek Fung's answer, if your drive having a login session you may need to provide the session access as well.

But If you use the view only url the user is not propted to login to there google account.

Simply you can use like this::

 webView.loadUrl("https://docs.google.com/viewer?url="+ pdfURL);

Upvotes: 2

Derek Fung
Derek Fung

Reputation: 8211

The link you try to use requires authentication, it means that unless the webview has the login session, you cannot access the content probably.

You have to follow the link below and setup either as Anyone with the link or Public web access, please be reminded that the URL would be something else as well.

Edit:

forget to provide the link

https://support.google.com/drive/answer/2494886?hl=en

Upvotes: 1

Related Questions