VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/android-jetpack-compose-retrieve-image-from-firebase-in-realtime/

⇱ Android Jetpack Compose - Retrieve Image From Firebase in Realtime - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Android Jetpack Compose - Retrieve Image From Firebase in Realtime

Last Updated : 23 Jul, 2025

When we are creating an android app then instead of inserting an image manually we want to get that from the internet and using this process the app size will become less. So, using firebase we can do this. We can create our storage bucket and we can insert our image there and get it directly into our app. But what if we want to change that image and instead of that image we want to insert the new one then for doing this we have to change our code but here our solution comes we can get that image in realtime in our app using real-time database then we can change that image from firebase and in real-time, our app image will also change we don’t have to do changes in our code. In this article, we will take a look at How to retrieve images from the Firebase Realtime database in Android using Jetpack Compose. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Connect your app to Firebase

After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot.  

πŸ‘ Image
 

Inside that column Navigate to Firebase Realtime Database. Click on that option and you will get to see two options on Connect app to Firebase and Add Firebase Realtime Database to your app. Click on Connect now and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase.

πŸ‘ Image
 

After completing this process you will get to see the below screen.

πŸ‘ Image
 

Now verify whether your app is connected to Firebase or not. Go to your build.gradle file. Navigate to the app > Gradle Scripts > build.gradle file and make sure that the below dependency is added in your dependencies section.

implementation platform('com.google.firebase:firebase-bom:30.3.2')
 implementation 'com.google.firebase:firebase-database-ktx'
 // added dependency below to load image from URL 
 implementation("io.coil-kt:coil-compose:2.0.0-rc01")

If the above dependency is not added to your dependencies section. Add this dependency and sync your project.

Step 3: Add internet permission to your AndroidManifest.xml file

Add the permission for the internet in the AndroidManifest.xml file. 

Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

After adding this code to your app. Now go to Firebase and click on Go to console option which is in the top right corner.  

πŸ‘ Image
 

 After clicking on this screen you will get to see the below screen with your all project inside that select your project.  

πŸ‘ Image
 

Inside that screen click n Realtime Database in the left window.  

πŸ‘ Image
 

After clicking on this option you will get to see the screen on the right side. On this page click on the Rules option which is present in the top bar. You will get to see the below screen.

πŸ‘ Image
 

In this project, we are adding our rules as true for reading as well as a write because we are not using any authentication to verify our user. So we are currently setting it to true to test our application. After changing your rules. Click on the publish button at the top right corner and your rules will be saved there. Now again come back to the Data tab. Now we will be adding our data to Firebase manually from Firebase itself. Now again come back to the Data tab. Now we will be adding our data to Firebase manually from Firebase itself.

Step 5: Adding URL for your Image in Firebase Console

Inside Firebase Realtime Database. Navigate to the Data tab. Inside this tab Hower on the database section inside click on the "+" icon. After clicking on the "+" icon you will get to see two input fields which are the Name and Value fields. Inside the Name field, you have to add a reference for your image file which in our case is "URL". And in our value field, we have to add a URL for our image file. After adding the value in this field. Click on Add button and your data will be added to Firebase Console. After adding the URL for your video. Now run your app and see the output of the app below :

Output:

You can change the URL of your image dynamically from the database and the image will be updated inside the application in real-time. 

Comment

Explore