CLOSE ✕
Get in Touch
Thank you for your interest! Please fill out the form below if you would like to work together.

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form

Upload image to Firebase Storage

Neelanshi Sharma
|
Firebase
|
Mar 1, 2021

Cloud Storage stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud. This allows you the flexibility to upload and download files from mobile clients via the Firebase SDKs, and do server-side processing such as image filtering or video transcoding using Google Cloud. Cloud Storage scales automatically, meaning that there's no need to migrate to any other provider.

Dependencies

In your android project add these dependencies to work with firebase storage.

implementation 'com.google.firebase:firebase-storage:19.1.1'

Implementation

1. Add following components to your XML file:

Button (@+id/select): To select the image from device

ImageView: To display the selected image

TextView: To display the name of the image

Button(@+id/buttonNext): To upload the image and start next activity (if any)

2. Make an entry for select button in onClick() method of your java class. This creates a new Intent instance. The intent type is set to image, and its action is set to get some content. The intent creates an image chooser dialog that allows the user to search through the device gallery to select the image from the gallery.

3. startActivityForResult is used to receive the result, which is the selected image. To display this image, make use of a method called onActivityResult(). onActivityResult receives a request code, result code, and the data. Check in this method, if the request code equals PICK_IMAGE_REQUEST, with the result code equal to RESULT_OK and the data available. If all this is true, display the selected image in the ImageView below buttons.

4. In uploadImage() method, Firebase storage reference is taken and putFile() function is used to upload the image to firebase storage with success and failure listeners. If an image is uploaded than success toast is there otherwise failure toast is there.

The uploaded images can be found in the Storage section of Firebase under the folder /images.

Neelanshi Sharma
Neelanshi is a mobile and web developer. She is a computer science major from Banasthali Vidyapith, India.

Recent Blog Posts

Lets Work Together
Contact Me