How can I use PDFViewer to open a pdf within Webview in Android

471 views Asked by At

I have been trying to create an Android app that will open a website in WebView. For Example, I would like to open Google.com and navigate to a file that is a pdf. I want to be able to open the pdf within the app instead of downloading it and opening it locally.

I have implemented PDFViewer within my code, so that eventually I will be able to open a pdf in PDFViewer within the app, and then navigate back to website when I am done using the back button all without ever navigating away from the app.

In the activity_main.xml file, I coded the PDFViewer underneath the Webview, so that it may open the pdf when it is called, but now I only get a blank page. I would appreciate any help that I can get!!

Below is my MainActivity.java file


import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import com.github.barteksc.pdfviewer.PDFView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private WebView mywebView;
    PDFView pdfView;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView) this.findViewById(R.id.webView);
        WebSettings webSettings= mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebView.loadUrl("www.google.com");

                if (url.endsWith(".pdf")) {
                    PDFView pdfView = findViewById(R.id.pdfView);
                    new RetrivePdffromUrl().execute(url);

                } else {
                    mywebView.loadUrl(url);
                }
    }


    class RetrivePdffromUrl extends AsyncTask<String, Void, InputStream> {
        @Override
        protected InputStream doInBackground(String... strings) {
         
            InputStream inputStream = null;
            try {
                URL url = new URL(strings[0]);
                
                HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                if (urlConnection.getResponseCode() == 200) {
                   
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }

            } catch (IOException e) {
               
                e.printStackTrace();
                return null;
            }
            return inputStream;
        }

        @Override
        protected void onPostExecute(InputStream inputStream) {
            pdfView.fromStream(inputStream).load();
        }
    }

    @Override
    public void onBackPressed() {
        if(mywebView.canGoBack()){
            mywebView.goBack();
        }else{
            super.onBackPressed();
        }

    }
}

And this is my activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </WebView>
</RelativeLayout>

I am fairly new to Android development, so I would appreciate any help that I can get. Thanks in advance!!!

1

There are 1 answers

0
Omkar On

I think theirs a small problem String url;

After that you directly used that without assigning in if statement

if(url.endsWith(".pdf"))

My apologies if I am wrong.