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 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.

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 ๐Ÿ™‚

 

49 thoughts on “Firebase Storage – Uploading and Downloading PDF Files”

  1. Outstanding Work Guys lot of Thank, Can you Please help to shave database between to app like merchant and customer if merchant upload customer can access that data, And how to make fire base Authentication using Email/Phone .. Once again Thanks

    Reply
  2. It is not downloading pdf file.How to download pdf file on Sdcard and How to read it from sdcard.Please help me as soon as possible

    Reply
  3. Im getting an error as compile import com.google.firebase.database.DatabaseReference; , import com.google.firebase.database.FirebaseDatabase; cannot resolve what to do for ths

    Reply
  4. Bhai My App Crashes when i open this section for Pdf Files. plz help me out with this problem and tell me which Exception is unhandled here.

    Reply
  5. Hi thanks for this tutorial, but if I want to let specific registered users to only see what they have uploaded, how should I implement it ? I tried changing the rules of storage and database, but then after that it says that I dont have permission to upload even though I am already logged in. Any advice ?

    Reply
  6. Please help me For uploading an Excel sheet to firebase using android programmatically

    And a common code for getting file for image , pdf , zip , excel and uploading to firebase in android

    Reply
  7. hi sir. your code is work perfectly with my project, but instead of we download back the uploaded file at the list, can we view it without downloading it?

    Reply
  8. UploadTask.TaskSnapshot getDownloadUrl() not working anymore! It used to and now it seems it is deprecated. Is there any alternative way to use your method though?

    Reply
  9. Getting an error when merging this uploading task with my project,the error is that after uploading file there is no file shown in list view ,can you help me

    Reply
  10. I am able to upload the PDF file to Firebase but i am not able to view and download the PDF file that is already stored in Firebase. please help me.

    Reply
  11. I am not able to download the pdf file..It only read the pdf but cannot download the file..Pls help…I want the code urgently

    Reply

Leave a Comment