![]() |
VOOZH | about |
Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides secure file uploads and downloads for Firebase application. This article explains how to build an Android application with the ability to select the image from the mobile gallery and upload images to Firebase Storage.
Note: Firebase Storage now requires a billing plane to use Firebase Storage.
Create a new project on android studio or open an existing project in which you want and add the firebase to that android application.
Navigate to Gradle Scripts > build.gradle.kts (Module:app) and add the following dependencies
dependencies {
...
implementation("com.google.firebase:firebase-storage-ktx:21.0.1")
}
Navigate to app > res > layout > activity_main.xml and add the following code
activity_main.xml:
Navigate to app > java > {package-name} > MainActivity.kt/.java and add the following code.
Set OnClickListeners on the buttons to handle user interaction. When the Select button is clicked, a image selection bottom sheet is displayed using ActivityResultLauncher with ActivityResultContracts.GetContent. This launches a system image picker letting the user to choose an image from their device's gallery. Once the image is selected, its URI is returned, which is then displayed in the ImageView below the buttons.
When the Upload button is clicked, the uploadImage() method is called. Inside this method, a Firebase Storage reference is initialized and a unique file path is created using a UUID. If an image is selected, it is uploaded to Firebase using the putFile() function. A progress dialog shows the upload progress.