Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webview will not load pdf files on link click

I have developed a web app that displays a list of pdf documents hosted on a web server. This app is embedded in a webview app for android however when I load the app on my phone, selection of a pdf link does nothing. What am I doing wrong? Thanks

Heres the java code:

package com.hellowebview;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HellowebviewActivity extends Activity {
/** Called when the activity is first created. */

private WebView mWebView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("http://aipnz.webs.com");
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

}
private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url)
    {
        webview.loadUrl(url);
        return true;
    }
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
    {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}
like image 200
Chris Avatar asked Feb 04 '12 09:02

Chris


People also ask

How do I open a PDF link in WebView?

Opening a PDF file in Android using WebView All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.

Why PDF is not opening in WebView Android?

Unfortunately, Android does not support viewing PDFs out of the box in a WebView. Luckily, Google has a nifty little tool that allows you to perform this very task quite easily using Google Docs. Basically we will embed our PDF in a Google Doc page on-the-fly and load that.

Can WebView load PDF?

We can load PDF in android easily using WebView. We will be using the simplest way for displaying PDF file in android. Using the Google Docs PDF viewer we could do this easily. Now lets get in to the implementation and source code.


3 Answers

If you want to load pdf you can use Google docs to load it.

String googleDocs = "https://docs.google.com/viewer?url=";
String pdf_url = "http://kronox.org/documentacion/Hello.Android.new.pdf";  

webView.loadUrl(googleDocs + pdf_url);

NOTE: You need android.permission.INTERNET in Manifest file

like image 186
Lalit Poptani Avatar answered Sep 28 '22 01:09

Lalit Poptani


You have to override shouldOverrideUrlLoading method in WebClient. I use this approach with combination of intent and Google Docs as a backup:

/* You might want to move this string definition somewhere else */
final String googleDocs = "https://docs.google.com/viewer?url=";

WebView webView = new WebView(getContext());
//webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if (url.endsWith(".pdf")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "application/pdf");
            /* Check if there is any application capable to process PDF file. */
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            } else {
                /* If not, show PDF in Google Docs instead. */
                view.loadUrl(googleDocs + url);
            }
        } else {
            webView.loadUrl(url);
        }
        return true;
    }
});

You might need to change passing the context and accessing startActivity method, but other than that it should run as it is.

Also note that since API 24, there are 2 shouldOverrideUrlLoading methods you can override. As it is stated here from @CommonsWare, it is OK to override deprecated method.

like image 42
arenaq Avatar answered Sep 27 '22 23:09

arenaq


This is the solution I use:

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".pdf")){
            String pdfUrl = googleDocs + url;
            view.loadUrl(pdfUrl);
        } else {
            view.loadUrl(url);
        }
        return true;
    }

with

private final String googleDocs = "https://docs.google.com/viewer?url=";
like image 39
bustazone Avatar answered Sep 28 '22 01:09

bustazone