Skip to content

Getting Started with Firebase Extensions for Android (Firebase Console Edition)

Firebase once again released a new addition to their multitude of tools and services and this tool is, well, A LOT of tools in one. In a package they call Firebase Extensions, we have access to a number of automated tasks that we can install directly from Firebase instead of having to invest time and costs in writing them ourselves. These extensions include things like text translation, resizing images uploaded to Cloud Storage, sending emails when data is added to Cloud Firestore, URL shortening, Mailchimp syncing, yeah there’s quite a few.

A few of the Firebase Extensions found on the official page

Firebase Extensions can be configured through either the Firebase Console or the Firebase CLI. Since it’s so easy to do in the Console, we’ll start with that.

We’re going to learn how to use them through the Resize Images extension as it’s one of the easier ones to get started with.

Install the Dependencies

If you haven’t already, connect your app to Firebase, then add these dependencies to your app/build.gradle file.

implementation 'com.google.firebase:firebase-storage:19.1.0'
implementation 'com.google.firebase:firebase-functions:19.0.1'

Just like most extensions, this one makes use of other Firebase services. The Resize Image extension uses Cloud Storage to, well, upload the image to storage and Cloud Functions to perform the resizing on the cloud.

Extensions however doesn’t need to add itself as a dependency in your app as it works exclusively on the cloud.

Configure the Firebase Extension

Head into the Firebase Console and on the left side bar, click on Extensions. Look for the Resize Images extension, and follow the process to get it set up.

I chose to left most of the parameters default as they suffice. There’s also a parameter that lets you specify in which path of the Cloud Storage you want this extension to apply to. Leaving it blank makes it apply to the whole bucket, which I did as I wasn’t too concerned with resizing images in only a certain path.

Eitherway, the extension will trigger automatically and upon finishing the resize, it will save both the original and the resized image into your specified path.

So once you finish setting up your extension, it’ll take a few minutes to get loaded. Once it is, then it’s ready to do it’s job!

Set up Cloud Storage

All that’s left is for us to actually upload an image from within our app.

My layout consists of just a single button. All of the code here in my MainActivity just uses that button to upload an image to Cloud Storage. If you’re new to Firebase Cloud Storage, you’ll find my tutorial on it pretty helpful.

class MainActivity : AppCompatActivity() {

    val PICK_IMAGE = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_upload.setOnClickListener { startPickImageIntent() }
    }

    private fun startPickImageIntent() {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        intent.type = "image/*"
        startActivityForResult(intent, PICK_IMAGE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {
        super.onActivityResult(requestCode, resultCode, intentData)

        if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            intentData?.data?.let { uploadImageToCloudStorage(it) }
        }
    }

    private fun uploadImageToCloudStorage(imageUri: Uri) {
        val storage = FirebaseStorage.getInstance()
        val samplesRef = storage.reference.child("samples")

        val riversRef = samplesRef.child("images/" + imageUri.lastPathSegment!!)

        riversRef.putFile(imageUri)
            .addOnFailureListener {
            // Handle unsuccessful uploads
        }
            .addOnSuccessListener { taskSnapshot ->
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
            val downloadUrl = taskSnapshot.metadata?.path
        }
    }

}

We’re all done! Trigger the upload from within in the app by SMASHING that upload button we created and SLAPPING an image onto it and in no time, we’ll see that image along with a resized version of it in Cloud Storage where we specified our path.

That’s all for now. Happy Coding (づ。◕‿‿◕。)づ