Upload Images to Firebase Cloud Storage

In the Last Post, we talked about  How can we create an android app that can take a picture and save it into the gallery, and we also learned how to get the Full-Sized image URL from the gallery. While writing that article was fun but the main purpose of that article was to be able to upload the image that we have taken into Firebase and get its Downloadable URL to display in ImageView. 

So In this article, we will just upload the image into Firebase, we will talk about the basic things you need to know about uploading the images in the firebase store. 

If you have not read the first article, then it is recommended to do so before continuing here, 
anyway, let’s start. First, we will connect the firebase to Our Android app. 

This article is part of the SmallAcademy Android Tutorial video series, You can watch all the videos below: 

Scroll at the End of Article for Full Source Code. 

  • In Android Studio Select Tools >> Firebase then Select Storage options. 
  • Now, We Need to Connect To Firebase, click on it. 

If you are doing it for the first time, Android Studio will ask you to sign in to firebase with your Gmail. Once done come back to Android studio, you will have the option to create a new Firebase app on your screen. 

Now create a new App, and it will connect your app to firebase, We will setup this firebase storage later to upload the images. In the Meantime, lets go and write the code to upload the images into firebase. 

In Your MainActivity.Java, Create new Storage reference, 

StorageReference storageReference; 

Inside OnCreate() method , Initialize the “storagereference” :

storageReference = FirebaseStorage.getInstance().getReference();

Create a method in MainActivity.java, that will take two arguments. first, it will take the FileName of the images and Second the URL where the image is stored in the device. sot the Method name look like this

uploadImagesToFirebase(String name,Uri contentUri){ }

Now the complete method that uploads the images into firebase looks like this:

storageReference.child(“pictures/” + name);  specify the location in Firebase storage where our image will be saved. “Picture” is the collection and inside that image will be saved with the given name. 

getDownloadUrl() :  Return URL of the image that is uploaded into firebase, we can use this URL to download and display images in our app. 

For Simplicity, We will use Picasso Image Library to Display our image in ImageView. So goto Picasso Github Repository and get the dependency and add it to the Build.gradle file of our app and Sync the Project. 

Picasso.get().load(uri).into(selectedImage); This will load the image into imageView Whose variable name is selectedImage. 


Now Its time we go to firebase console and setup rules before running the app. When you log in to the firebase app for the first time, it will ask you to select the server and rules. 
  • Open Firebase Console
  • Select Storage From side Menu
Now You need to specify Rules. Select Production Rules and You will be redirected to Storage Dashboard which is empty as of now.
Now go to Rules Tab, there you need to add the following line of code.

allow read,write;

This basically allows anyone to read and write to our database since we are not using an authentication system. But please remember you should always use authentication before releasing app for production.

Now come back to android studio and Run the app. Select image to upload and it should be Displayed in the ImageView.

The Full Source code is Here.

We will be happy to hear your thoughts

Leave a reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.