简体   繁体   中英

How to open pdf file inside my app (android studio, JAVA) from firebase storage link

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.

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);

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();
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM