How to upload images to Firebase Storage





Hello Guys! In this tutorial, we’ll learn how to use Firebase Storage. We are going to build a very simple android app which contains and an ImageView and a Button. User will select an image file from his gallery and the selected image will get uploaded to the Firebase Storage on click of a Button. So, Let’s get started.

1.) Create a new project in Firebase console or choose an existing one. Click on Storage from the sidebar and copy the Firebase storage URL (eg: gs://fir-demo-d7354.appspot.com). Go to RULES tab and set the request.auth !=null to request.auth==null as we are not going to authenticate users. PUBLISH the changes after modifying.

1

2.) Create a new Android project “FirebaseStorageDemo”. Add the Firebase Storage to your app.

App level Gradle File

Add the below lines in your app level gradle file.

compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.google.firebase:firebase-storage:9.2.1'
compile 'com.google.firebase:firebase-auth:9.2.1'
apply plugin: 'com.google.gms.google-services'
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}


1

Project level gradle file

Add the  below classpath in the dependencies section.

classpath 'com.google.gms:google-services:3.0.0'

3.) Add your android app to the Firebase project by clicking ADD APP button in the Overview page of Firebase console. Enter the package name and SHA1 debug key. Download the google-services.json file and place it in your app/ directory of your Android project.

4.) Now, open the layout(activity_main) for your MainActivity and create an ImageView and a Button.

5.) Add the below code in your MainActivity.

6.) Run the app and select an Image from Gallery. On clicking the upload image button, the selected image will be uploaded to your Firebase Storage.

 

 

Screenshot_20160724-160907Screenshot_20160724-160916

 

1

 

That’s it for this tutorial. Thank you 🙂

DOWNLOAD THE COMPLETE PROJECT: https://github.com/cvivek07/FIrebaseStorageDemo

Issues? Please comment below.

admin Author

Comments

    Kartik

    (December 25, 2016 - 9:36 AM)

    I did exactly as above but getting this error

    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/internal/zzab;
    at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
    at com.paperwrrk.android.storageref.MainActivity.(MainActivity.java:36)

      admin

      (December 29, 2016 - 12:06 AM)

      Just update your google play services to the latest version and try again.

Leave a Reply to Kartik Cancel reply

Your email address will not be published. Required fields are marked *