How to Upload PDF Files in Firebase Storage in Android?
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 ‘com.google.firebase:firebase-storage:16.0.4’
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
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center" android:orientation = "vertical" tools:context = ".MainActivity" > < ImageView android:id = "@+id/uploadpdf" android:layout_width = "200dp" android:layout_height = "200dp" android:src = "@drawable/ic_picture_as_pdf_black_24dp" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Upload A Pdf" android:textSize = "22sp" android:textStyle = "bold" /> </ LinearLayout > |
Step 4: 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.
Java
import android.app.ProgressDialog; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.google.android.gms.tasks.Continuation; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.storage.FirebaseStorage; import com.google.firebase.storage.StorageReference; public class MainActivity extends AppCompatActivity { ImageView upload; Uri imageuri = null ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); upload = findViewById(R.id.uploadpdf); // After Clicking on this we will be // redirected to choose pdf upload.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent galleryIntent = new Intent(); galleryIntent.setAction(Intent.ACTION_GET_CONTENT); // We will be redirected to choose pdf galleryIntent.setType( "application/pdf" ); startActivityForResult(galleryIntent, 1 ); } }); } ProgressDialog dialog; @Override 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 ); dialog.setMessage( "Uploading" ); // this will show message uploading // while pdf is uploading dialog.show(); 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() { @Override public Object then( @NonNull Task task) throws Exception { if (!task.isSuccessful()) { throw task.getException(); } return filepath.getDownloadUrl(); } }).addOnCompleteListener( new OnCompleteListener<Uri>() { @Override public void onComplete( @NonNull Task<Uri> task) { if (task.isSuccessful()) { // After uploading is done it progress // dialog box will be dismissed dialog.dismiss(); Uri uri = task.getResult(); String myurl; myurl = uri.toString(); Toast.makeText(MainActivity. this , "Uploaded Successfully" , Toast.LENGTH_SHORT).show(); } else { dialog.dismiss(); Toast.makeText(MainActivity. this , "UploadedFailed" , Toast.LENGTH_SHORT).show(); } } }); } } } |
Output:
Firebase Storage:
Please Login to comment...