Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android load local(in Asset/Res/External Storage) pdf file in webview

I want to display pdf file in webview.
I try using this code it displays blank page.
File retrieve perfacly but not display in webView.
Put screen shoot of webview displays webview when i run the application.
display blank webview. like this....

enter image description here

private WebView view;
Uri path;

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

        view = (WebView) findViewById(R.id.webView);
        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Documents/MyPDFFILE.pdf");
        path = Uri.fromFile(file);
        view.setContentDescription("application/pdf");
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setLoadsImagesAutomatically(true);
        view.getSettings().setPluginsEnabled(true);
        view.getSettings().setAppCacheEnabled(true);
        view.loadUrl(path.toString());

        // view.loadUrl("http://grail.cba.csuohio.edu/~matos/notes/cis-493/lecture-notes/Android-Chapter10-WebKit.pdf");
    }
like image 654
Deval Patel Avatar asked Dec 31 '25 18:12

Deval Patel


1 Answers

Because, Android WebView doesn't support PDF file functionality. And WebView cannot render PDFs.

The URL you posted on your comments it displayed pdf file in native web browser because of it uses Google Docs.

If you want to display pdf file in your webview then using Google Docs pass url of your pdf file which located on server not in your local.

Update: Code for display pdf file located on net in webview.

WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "http://www.xxxx.com/xxxx/xxxx.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
like image 170
user370305 Avatar answered Jan 02 '26 10:01

user370305