Firebase Cloud Storage | Android App Development | Lecture#59 | Hive Learners

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Dear Hive Learners, In the previous lecture we learn how to get the Profile Image in an Image View. Today we will configure the FirebaseStorage so that we can save this image in our Firebase storage when the user signup. We will save the link to the image in our real-time database. When we successfully upload an image to Firebase Storage we will get a link to access the image.

GitHub Link

Use this GitHub project to clone into your directory. The following lecture will update it so you will never miss the latest code. Happy Coding!

What Should I Learn

  • How to add Firebase Cloud Storage to our peoject

Assignment

  • Configure Firebase Cloud SDK to your project

Procedure

First of all, we need to open the Firebase Assistance. This will help us to implement the dependencies in one click. Open the Cloud Storage section and connect your app to Firebase and click on Add the Cloud storage SDK to your app. This will implement the dependencies and sync the project. Wait for the project to sync successfully.

Click on Accept Changes.

Now declare and initialize the Firebase Storage and the Storage Reference variables.

    private FirebaseStorage storage = FirebaseStorage.getInstance();
    private StorageReference storageRef = storage.getReference();

Now we can use this storage reference to save the images in a child. Let's name it profile_images. We can implement the On Complete Listener and this time I am using null in place of File Uri as we do not pick the Uri yet.

 storageRef.child("profile_images").putFile(null).addOnCompleteListener(task -> {


        });

We can show the Progressbar when the file is uploading and cancel if the task is failed or successful.

storageRef.child("profile_images").putFile(null).addOnCompleteListener(task -> {
            progressDialog.setMessage("Please wait...");
            progressDialog.show();
            progressDialog.setCancelable(false);

            if (task.isSuccessful()) {
                if (progressDialog.isShowing()) {
                    progressDialog.cancel();
                    Toast.makeText(this, "Uploaded", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show();
                if (progressDialog.isShowing()) {
                    progressDialog.cancel();
                }
            }


        });

In the next lecture, we will create a function upload_image with the parameter Uril. This function will accept the Uri where is called. In the Activity result method, we will call save the Uri in a variable,, and on the profile creation time we can set the Uri in his function or we can use this Uri for the UserProfileChangeRequest.


hl_divider.png

Thank You

hl_footer_banner.png



0
0
0.000
2 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000