0

I have pdf file stored on my firebase storage, and its link in firebase real-time database, how can I open it inside my android app (android studio, JAVA). Not using INTENT , but directly inside my app.

2 Answers2

0

You can use android-pdfView, see this blog post. It demonstrates the basic usage of the library to display pdf onto the view with vertical and horizontal swipe.

pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromFile(new File("/storage/sdcard0/Download/pdf.pdf")).defaultPage(1).enableSwipe(true).onPageChange(this).load();

There are also other libraries like Android PDF Viewer, VuDroid etc.

Also Android API 19 provides feasibility now to present pdf content inside an app and thus no need of 3rd party SDKs.

you can find details here.

If you want to load the file using a URL then you can use a webview.

WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
udi
  • 3,672
  • 2
  • 12
  • 33
  • I want to open file from firebase storage link .....not from asset .....can you share any link which teaches the same ... – Jeet Raj Sahil Mar 24 '21 at 04:39
  • It works fine with every other url except firebase storage access token, its shows **no preview available**, i want to open file from firebase storage, can you test this code with firebase storage and let me know, probably I am doing something wrong – Jeet Raj Sahil Mar 24 '21 at 05:45
0

Found the answer with little research :- no need of webview or Intent which opens another app

Library :- implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

xml code :-

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

JAVA code :-

{
 String pdfurl="firebase_access_token_of_pdf_file";

 pdfView = (PDFView) findViewById(R.id.pdfView);
 new RetrivePDFfromUrl().execute(pdfUrl);
}

 // create an async task class for loading pdf file from URL.
class RetrivePDFfromUrl extends AsyncTask<String, Void, InputStream> {
    @Override
    protected InputStream doInBackground(String... strings) {
        // we are using inputstream
        // for getting out PDF.
        InputStream inputStream = null;
        try {
            URL url = new URL(strings[0]);
            // below is the step where we are
            // creating our connection.
            HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            if (urlConnection.getResponseCode() == 200) {
                // response is success.
                // we are getting input stream from url
                // and storing it in our variable.
                inputStream = new BufferedInputStream(urlConnection.getInputStream());
            }

        } catch (IOException e) {
            // this is the method
            // to handle errors.
            e.printStackTrace();
            return null;
        }
        return inputStream;
    }

    @Override
    protected void onPostExecute(InputStream inputStream) {
        // after the execution of our async
        // task we are loading our pdf in our pdf view.
        pdfView.fromStream(inputStream).load();
    }
}
}