Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Upload PDF Files in Firebase Storage in Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides secure file uploads and downloads for the Firebase application. This article explains how to build an Android application with the ability to select the pdf from the mobile phone and upload that pdf to the Firebase Storage.

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. And add firebase to your project or you can refer to Adding Firebase to Android App.

Step 2: Add the dependency in the build.gradle (Module : app) file

Navigate to the Gradle Scripts > build.gradle (Module : app) and add the below dependency in the dependencies section. 

implementation ‘’

implementation fileTree(dir: ‘libs’, include: [‘*.jar’])

After adding this dependency sync your project and now we will move towards its implementation.  

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
        android:src="@drawable/ic_picture_as_pdf_black_24dp" />
        android:text="Upload A Pdf"
        android:textStyle="bold" />

Step 4: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class MainActivity extends AppCompatActivity {
    ImageView upload;
    Uri imageuri = null;
    protected void onCreate(Bundle savedInstanceState) {
        upload = findViewById(;
        // After Clicking on this we will be
        // redirected to choose pdf
        upload.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent galleryIntent = new Intent();
                // We will be redirected to choose pdf
                startActivityForResult(galleryIntent, 1);
    ProgressDialog dialog;
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            // Here we are initialising the progress dialog box
            dialog = new ProgressDialog(this);
            // this will show message uploading
            // while pdf is uploading
            imageuri = data.getData();
            final String timestamp = "" + System.currentTimeMillis();
            StorageReference storageReference = FirebaseStorage.getInstance().getReference();
            final String messagePushID = timestamp;
            Toast.makeText(MainActivity.this, imageuri.toString(), Toast.LENGTH_SHORT).show();
            // Here we are uploading the pdf in firebase storage with the name of current time
            final StorageReference filepath = storageReference.child(messagePushID + "." + "pdf");
            Toast.makeText(MainActivity.this, filepath.getName(), Toast.LENGTH_SHORT).show();
            filepath.putFile(imageuri).continueWithTask(new Continuation() {
                        public Object then(@NonNull Task task) throws Exception {
                            if (!task.isSuccessful()) {
                                throw task.getException();
                            return filepath.getDownloadUrl();
                    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful()) {
                        // After uploading is done it progress
                        // dialog box will be dismissed
                        Uri uri = task.getResult();
                        String myurl;
                        myurl = uri.toString();
                        Toast.makeText(MainActivity.this, "Uploaded Successfully", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "UploadedFailed", Toast.LENGTH_SHORT).show();


Firebase Storage:

Github Link.

My Personal Notes arrow_drop_up
Last Updated : 22 Feb, 2021
Like Article
Save Article
Similar Reads
Related Tutorials