Open In App

How to Convert Drawable to Bitmap in Android using Jetpack Compose?

Improve
Improve
Like Article
Like
Save
Share
Report

A drawable is an image file that is used to display inside our image view. We can display the images directly within the image view from our drawable folder. Images can be displayed within our image view directly in the form of the image file or a bitmap format. In this article, we will take a look at How to Convert Drawable to Bitmap 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: Adding a new color in the Color.kt file

Navigate to the app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.  

Kotlin




package com.example.newcanaryproject.ui.theme
  
import androidx.compose.ui.graphics.Color
  
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
  
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)


Step 3: Adding images to the drawable folder

Copy the image which you want to add to your image view. Navigate to app > res > drawable. Right-click on it and paste all the images into the drawable folder.

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.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.newcanaryproject
  
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Bundle
import android.util.Log
import android.widget.Space
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import androidx.core.content.ContextCompat
import com.example.newcanaryproject.ui.theme.*
  
class MainActivity : ComponentActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying background color for our application
                Surface(
                    // on below line we are specifying modifier and color for our app
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
                ) {
  
                    // on below line we are specifying theme as scaffold.
                    Scaffold(
  
                        // in scaffold we are specifying top bar.
                        topBar = {
  
                            // inside top bar we are specifying background color.
                            TopAppBar(backgroundColor = greenColor,
  
                                // along with that we are specifying title for our top bar.
                                title = {
  
                                    // in the top bar we are specifying tile as a text
                                    Text(
  
                                        // on below line we are specifying 
                                        // text to display in top app bar.
                                        text = "Image to Bitmap",
  
                                        // on below line we are specifying 
                                        // modifier to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
  
                                        // on below line we are 
                                        // specifying text alignment.
                                        textAlign = TextAlign.Center,
  
                                        // on below line we are 
                                        // specifying color for our text.
                                        color = Color.White
                                    )
                                })
                        }) {
                        // on below line we are calling drawable to
                        // bitmap method to convert drawable to bitmap.
                        drawableToBitmap()
                    }
                }
            }
        }
    }
}
  
// on below line we are creating a composable
// function to display ui for drawable to bitmap. 
@Composable
fun drawableToBitmap() {
    // on below line we are creating 
    // a variable for context
    val ctx = LocalContext.current
  
    // on below line we are creating a column for our ui.
    Column(
        // in this column we are adding a modifier for our 
        // column and specifying max width, height and size.
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .fillMaxSize()
  
            // on below line we are adding padding 
            // from all sides to our column.
            .padding(6.dp),
  
        // on below line we are adding vertical 
        // arrangement for our column as bottom
        verticalArrangement = Arrangement.Center,
  
        // on below line we are adding horizontal
        // alignment for our column.
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // on below line we are displaying a simple text
        Text(
  
            // on below line we are specifying
            // modifier as padding for our text.
            modifier = Modifier.padding(6.dp),
  
            // on below line we are specifying
            // text as normal image. 
            text = "Normal Image",
  
            // on below line we are specifying
            // font weight as bold. 
            fontWeight = FontWeight.Bold,
  
            // on below line we are 
            // setting color for our text
            color = greenColor,
  
            // on below line we 
            // are setting font size. 
            fontSize = 20.sp
        )
  
        // on below line we are creating a simple spacer with height 20 
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are creating an image for our simple image. 
        Image(
            // in this image we are specifying our drawable file 
            // which we have to display on below line.
            painter = painterResource(id = R.drawable.android),
  
            // on below line we are specifying 
            // content description for our image. 
            contentDescription = "Android",
  
            // on below line we are specifying 
            // alignment for our image. 
            alignment = Alignment.Center
        )
  
        // on below line we are specifying spacer of height 20
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are displaying a simple text
        Text(
  
            // on below line we are specifying 
            // modifier as padding for our text.
            modifier = Modifier.padding(6.dp),
  
            // on below line we are 
            // specifying text as bitmap image. 
            text = "Bitmap Image",
  
            // on below line we are specifying
            // font weight as bold. 
            fontWeight = FontWeight.Bold,
  
            // on below line we are 
            // setting color for our text
            color = greenColor,
  
            // on below line we
            // are setting font size. 
            fontSize = 20.sp
        )
  
        // on below line we are specifying spacer of height 20 
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are creating a variable for bitmap 
        // and initializing it by calling get bitmap from image method. 
        val bitmap = getBitmapFromImage(ctx, R.drawable.android)
  
        // on below line we are creating our bitmap image/ 
        Image(
            // on below line we are adding modifier 
            // to add height and width for our image. 
            modifier = Modifier
                .height(200.dp)
                .width(200.dp),
  
            // on below line we are adding bitmap 
            // for our image on below line. 
            bitmap = bitmap.asImageBitmap(),
  
            // on below line we are setting 
            // content description for our image.
            contentDescription = "Android",
  
            // on below line we are adding
            // alignment for our image
            alignment = Alignment.Center
        )
    }
}
  
// on below line we are creating a function to get bitmap 
// from image and passing params as context and an int for drawable. 
private fun getBitmapFromImage(context: Context, drawable: Int): Bitmap {
  
    // on below line we are getting drawable
    val db = ContextCompat.getDrawable(context, drawable)
  
    // in below line we are creating our bitmap and initializing it. 
    val bit = Bitmap.createBitmap(
        db!!.intrinsicWidth, db.intrinsicHeight, Bitmap.Config.ARGB_8888
    )
  
    // on below line we are 
    // creating a variable for canvas. 
    val canvas = Canvas(bit)
  
    // on below line we are setting bounds for our bitmap.
    db.setBounds(0, 0, canvas.width, canvas.height)
  
    // on below line we are simply 
    // calling draw to draw our canvas. 
    db.draw(canvas)
  
    // on below line we are
    // returning our bitmap. 
    return bit
}


Now run your application to see the output of it. 

Output: 

Output

 



Last Updated : 19 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads