Sunday, April 29, 2018

Android Tutorial How To Insert Image Into Firebase Storage



Lately, Google has recommend to use Firebase as your storage when you build an application (web or mobile application). Firebase also known as serverless. Serverless means you no more need API for transfer/send data, with firebase it will be done.

Firebase has much features that can use in you application, there are :
  • Firebase Realtime message
  • Firebase Cloud Firestore
  • Firebase Cloud Function
  • Firebase Storage.
You can read all of these features in firebase official website to get more information about those features.

In this article I will show you how to insert image into Firebase Storage. 


Create your application On Firebase


1. Go to Firebase Console and create your project


2. Choose your platform (Choose Android)


3. Add your SHA-1 Key and your package name


4. Download the google-services.json


5. And place the google-services.json inside you app folder



Gradle Setting


Now let setup your gradle and add some dependencies of Firebase

build.gradle (Project:YourProjectName)

build.gradle (Module:App)

Ok your gradle has been setup, now turn to create the process how to insert image into Firebase Storage.


Java And XML Code


activity_main.xml

MainActivity.java

Chose your image from gallery or taken from camera using Edmodo Image Cropper


After image taken, process the image inside method OnActivityResult and upload it into Firebase Storage


Take a look at this code :


Here is the result of the above code :


That code save your image into path photo_profile/my_photo.jpg (last child will be set as the name of the image). By using this path (same path), the new image that you store will replace the old image, why? Because you put the file into the same path like before.

So, how to store much image into Firebase Storage? Easy. Just replace the last child using Current Milliseconds


The result of the above code will be like this :


After your image has stored into Firebase Storage, now use the following code to display the image into your ImageView

Full Code of MainActivity :

Run your application, select image and click button upload, then the image will stored into Firebase Storage, Goodluck!

Related Posts


EmoticonEmoticon