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.
Asked
Active
Viewed 1,498 times
0
-
Use `WebView` if want a simple solution or checkout [PdfRenderer](https://developer.android.com/reference/android/graphics/pdf/PdfRenderer) – Hussain Mar 24 '21 at 04:30
-
how to use WebView – Jeet Raj Sahil Mar 24 '21 at 04:37
-
[This](https://stackoverflow.com/questions/2655972/how-can-i-display-a-pdf-document-into-a-webview) might help – Hussain Mar 24 '21 at 04:52
-
nope....not helping – Jeet Raj Sahil Mar 24 '21 at 08:46
2 Answers
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();
}
}
}

Jeet Raj Sahil
- 99
- 2
- 6