How to Upload Images to Firebase Storage | Android App Development Tutorial

In the Last Post we talked about  How can we create an android app that can take picture and save into gallery, and we also learned how to get the Full Sized image URL from 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 firebase store. 

If you have not read the first article, then it is recommended to do so before continuing here, 
anyway lets 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 sigh in to firebase with your Gmail. Once done comeback to Android studio, you will have option to create new firebase app in your screen. 

Now Create new App, and it will connect your app to firebase, We will setup this firebase storage later to upload the images. In 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 argument. first it will take the FileName of the images and Second the URL where the image is stored in device. sot the Method name look like this

uploadImagesToFirebase(String name,Uri contentUri){ }


Now the complete method that upload 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 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 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. 


SETUP FIREBASE TO UPLOAD IMAGE : 

Now Its time we go to firebase console and setup rules before running the app. When you login into 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 following line of code. 
allow read,write;
This basically allow anyone to read and write to our database, since we are not using authentication system. But Please remember you should always use authentication before releasing app for production.

Now comeback 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.
























No comments

Powered by Blogger.