Convert Drawable to Bitmap Android code

Last updated Apr 03, 2021


Showing images from internet get time to load on the screens, some times we need to show images from the drawable folder on dynamically. We can show images from drawable folder by setImageResource() and setImageBitmap() methods. In this post we will cover how to convert drawable to bitmap in android. we will show you convert drawable to bitmap and append this bitmap image to Imageview.

Let's get started

Step 1: Create Android application in Android studio

Step 2: Update xml file with below code

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ConvertDrawableToBitmap">
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginTop="50dp"
        android:textSize="20sp"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"


        app:layout_constraintStart_toStartOf="parent" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="300dp"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView2"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Convert Bitmap"
        android:layout_marginTop="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

To Convert Drawable to Bitmap we will use BitmapFactory.decodeResource() property. This property takes two arguments.

  • Resource Instance
  • Resource ID

 

val bitmap = BitmapFactory.decodeResource(resources, R.drawable.control_staements)

 

 

Step 3: Update Activity code with below code

 

package com.rrtutors.kotlincource


import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity


class ConvertDrawableToBitmap : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_convert_drawable_to_bitmap)
        val button=findViewById<Button>(R.id.button)
        val imageView=findViewById<ImageView>(R.id.imageView)
        val textView2=findViewById<TextView>(R.id.textView2)
        button.setOnClickListener {
            textView2.text=" Code:\n val bitmap = BitmapFactory.decodeResource \n (resources, R.drawable.control_staements)"
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.control_staements)
            imageView.setImageBitmap(bitmap)
            Toast.makeText(
                applicationContext, "Image converted to Bitmap",
                Toast.LENGTH_SHORT
            ).show()
        }
    }
}

 

Step 4: Add below code in manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rrtutors.kotlincource">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.KotlinCource">
        <activity android:name=".ConvertDrawableToBitmap"
            android:label="Convert Drawable to Image"
            >
            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
      
    </application>

</manifest>

 

Step 5: Run Application

 

Convert Drawable to Bitmap android

 

Related

How to avoid multiple button click at same time in android?

How to generate signed apk with Android Studio to publish on Playstore

 

Tags: Drawable, Bitmap, Convert Drawable to Bitmap

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

358 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions