Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a PDF file inside a WebView

Around 2 days I tried to open PDF files in my custom WebvView. Here's my WebView code:

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebActivity extends AppCompatActivity {
    public
    WebView mywebViewfull;
    ProgressBar progressBar;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        String url = getIntent().getStringExtra("url");
        mywebViewfull = (WebView) findViewById(R.id.webnav1);
        progressBar=(ProgressBar)findViewById(R.id.progressbar1);
        WebSettings webSettings= mywebViewfull.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setGeolocationEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSaveFormData(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setGeolocationDatabasePath(getFilesDir().getPath());
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        mywebViewfull.loadUrl(url);
        mywebViewfull.setWebViewClient(new mywebViewfullClient());


    }


    @Override
    public void onBackPressed() {
        if(mywebViewfull.canGoBack())
        {
            mywebViewfull.goBack();
        }

        else
        {
            super.onBackPressed();
        }
    }
    class mywebViewfullClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
       // android.support.v7.app.ActionBar actionBar = getSupportActionBar();
       // actionBar.hide();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && url.startsWith("http://")) {
            //open url contents in webview
            return false;
        }if (url != null && url.startsWith("https://")) {
                //open url contents in webview
            return false;
              //this must open pdf file
        }if (url.endsWith(".pdf")) {
            mywebViewfull.loadUrl("https://docs.google.com/gview?embedded=true&url="+getIntent().getStringExtra("url"));
            return false;

        }
        else {
            //external links in app
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;

        }
    }
}
    //back previous page
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction()==KeyEvent.ACTION_DOWN){
            switch (keyCode){
                case KeyEvent.KEYCODE_BACK:
                    if (mywebViewfull.canGoBack()) {
                        mywebViewfull.goBack();
                    }
                    else  {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);


    }

}

So... When I try to open pdf document, nothing happens in my app.

I can't see what's wrong! Can anyone help me?

like image 659
Douglas Filipe Avatar asked Feb 14 '18 17:02

Douglas Filipe


1 Answers

There is no native support to open PDF files in Android, but you have some workarounds that can more or less do the job for you. Alternatives:

1 - You could open your system default app to read the pdf file by an intent from your app;

2 - You could use a third party library to open the pdf inside your app;

3 - You could invoke Google Docs website functionality to open your pdf file inside your webview. To do so, use the following code sample:

String pdfUrl = "http://yourwebsite.com/yourfile.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
like image 137
statosdotcom Avatar answered Oct 11 '22 13:10

statosdotcom