Firebase Storage – Uploading and Downloading PDF Files

I got many messages asking how to store and fetch PDF files in Firebase Storage. So here is the detailed post explaining about Uploading PDF files in Firebase Storage and Fetching those uploaded files from …

firebase storage

I got many messages asking how to store and fetch PDF files in Firebase Storage. So here is the detailed post explaining about Uploading PDF files in Firebase Storage and Fetching those uploaded files from Firbase Storage. I already posted a tutorial about the same thing but with image files. You can check that post from the below link.

Firebase Storage – Uploading and Retrieving Images

Important: If you have no idea about Firebase Database and Firebase Storage, then it is highly recommended that go through the previous post from the link given above first. As I will not be explaining here about setting ab Firebase Storage and Database in the project. These things are well explained in the post given above. So please go through that first. 

Creating a New Project

  • So again we will start by creating a new project. I just created a project named FirebaseStorage.
  • After creating the project add Firebase Storage and Firebase Database to your project (Go to tools->firebase).

Creating UI

  • We have only two screens for this application. The first is the activity_main.xml which is created by default. And create one more Activity named ViewUploadsActivity.java.
  • In the first activity we will upload files to firebase storage and in the second activity we will display a list of all the uploaded files.

MainActivity

  • Open your activity_main.xml and write the following xml code.

  • The above code will generate the following screen.
activity_main.xml
activity_main.xml
  • You can see the layout is pretty simple, we have an EditText to get the file name, A button to upload the file and a TextView to go to the View Upload screen.

View Uploads Screen

  • As I told you that we have two screens in this app so create one more empty activity and name it ViewUploadsActivity.
  • Now come inside the layout file for this activity and write the following xml code.

Creating Helper Classes

  • For this we need some helper classes for modeling the data and storing the constants. So first create a class named Constants.java and write the following code.

  • Now to save the uploads data in Firebase database we need a model class. So create a new class named Upload.java and write the following code.

  • Now lets upload the files.

Uploading PDF in Firebase Storage

  • Now here comes the main task of this post which is uploading PDF files to the storage. The uploading is done on the MainActivity so come inside MainActivity.java and write the following code.

  • Now you can test uploading a File. But before we need to change the Storage Rules and Database Rules on Firebase Console. As the default rules allows only authenticated users to upload the file and write to the database. So either you need to make an authentication on the app or change the rules to public. So for this example we will make the rules as public.

Changing Firebase Storage and Database Rules

Firebase Storage Rules

  • Go to Firebase Storage and in Firebase Console and change the rules as below.

Firebase Database Rules

  • Now for the Database Rules use the following.

Testing the Upload

  • Now lets test the application by uploading a file.
firebase storage upload pdf
Uploading PDF in Firebase Storage

Retrieving Files from Firebase Storage

  • Now come to the ViewUploadsActivity.java and write the following code.

  • Now test this screen as well.

firebase storage fetching pdf files

  • Bingo! it is working as well. When you will click on an Item it will download the uploaded file from the storage.

Download Source Code

  • If you are facing any troubles you can get my source code from here.

[sociallocker id=1372] Firebase Storage – Upload and Download PDFs Source Code [/sociallocker]

So thats all for this tutorial friends. If you are having any queries regarding Firebase Storage or Android Development don’t hesitate to comment. And if you want some help if you are stuck in your app then also comment here and I will try to post a tutorial to help you.

If you liked this Firebase Storage tutorial, I request you to please SHARE this post among your friends. If you don’t like it please comment the reason so that I can improve the website. So thats all for now. Thank You 🙂

 

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x