How to Retrieve PDF File From Firebase Realtime Database in Android?
When we are creating an android app then instead of inserting a pdf manually we want to fetch the pdf using the internet from firebase. Firebase Realtime Database is the backend service which is provided by Google for handling backend tasks for your Android apps, IOS apps as well as your websites. It provides so many services such as storage, database, and many more. The feature for which Firebase is famous is its Firebase Realtime Database. By using Firebase Realtime Database in your app you can give live data updates to your users without actually refreshing your app. We will be creating our storage bucket and we can insert our pdf there and get it directly into our app.
What we are going to build in this article?
We will be creating two Activities in this project. In one activity there will e a single Button and in another activity, we are viewing the pdf file. So when the user will click on the Button an AlertBox will be shown with there options “Download“, “View“, and “Cancel“. So the user will choose whether he/she want to View or Download the pdf. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Connect your app to Firebase
After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot.
Inside that column Navigate to Firebase Realtime Database. Click on that option and you will get to see two options on Connect app to Firebase and Add Firebase Realtime Database to your app. Click on Connect now and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase.
After completing this process you will get to see the below screen.
Now verify that your app is connected to Firebase or not. Go to your build.gradle file. Navigate to the app > Gradle Scripts > build.gradle file and make sure that the below dependency is added in your dependencies section.
If the above dependency is not added in your dependencies section. Add this dependency and sync your project. Now we will move towards the XML part of our app. Also, add the following dependency.
Now sync the project from the top right corner option of Sync now.
Step 3: Add Internet permission in the AndroidManifest.xml file
Navigate to the AndroidManifest.xml file and add the below permission for getting internet permission in the app.
<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” />
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
Step 4: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
Step 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 6: Create a new ViewpdfActivity class
Please refer to How to Create New Activity in Android Studio and name the activity as ViewpdfActivity. This activity is used for viewing the pdf file.
Step 7: Working with the activity_view_pdf.xml file
Navigate to the app > res > layout > activity_view_pdf.xml and add the below code to that file. Below is the code for the activity_view_pdf.xml file.
Step 8: Working with the ViewpdfActivity.java file
Go to the ViewpdfActivity.java file and refer to the following code. Below is the code for the ViewpdfActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 9: Adding pdf on firebase storage and copy the link of that pdf
In firebase go to the Storage option then click on Get Started button
After that click on the Upload file option to insert a pdf on firebase storage.
After that click on the pdf that you inserted then the from pdf details come in the right section then click on the access token and copy the pdf URL.
Step 10: Add that pdf URL to the real-time database
Go to the Realtime database option then add these values to the database. Inside that screen click on Realtime Database in the left window.
After clicking on this option you will get to see the screen on the right side. On this page click on the Rules option which is present in the top bar. You will get to see the below screen.
In this project, we are adding our rules as true for reading as well as a write because we are not using any authentication to verify our user. So we are currently setting it to true to test our application. After changing your rules. Click on the publish button at the top right corner and your rules will be saved there. Now again come back to the Data tab. Now we will be adding our data to Firebase manually from Firebase itself. Inside Firebase in the Data tab, you are getting to see the below screen. Hover your cursor on null and click on the “+” option on the right side and click on that option. After clicking on that option. Add the data as added in the below image. Make sure to add “pdf” in the Name field because we are setting our reference for Firebase as “pdf”. So we have to set it to “pdf”. You can change your reference and also change it in the Database. Inside the value field, paste the copied pdf url. This will be the string which we are going to display inside our pdfview. After adding data click on the add button and your data will be added in Firebase and this data will be displayed in your app.