Convert drawable image into bitmap in android programmatically

Get local drawable resource imageview image and convert into bitmap type dynamically using coding on button click.

Bitmap image type is also a image extension mostly used in android applications to create and store drawable images inside android projects. So here is the complete step by step tutorial for Convert drawable image into bitmap in android programmatically.

android-project-download-code-button

Note: Copy image inside drawable-hdpi folder.

drawable hdpi folder

Download sample_drawable_image from below and copy inside drawable-hdpi folder.

sample_drawable_image

How to Convert drawable image into bitmap in android programmatically.

Code for MainActivity.java file.

 package com.drawabletobitmap_android_examples.com;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

 ImageView image;
 Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 image = (ImageView)findViewById(R.id.imageView1);
 button = (Button)findViewById(R.id.button1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 // Getting drawable image via drawable-hdpi folder and covert into bitmap.
 
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.sample_drawable_image);

 //Setting up converted bitmap image inside imageview.
 image.setImageBitmap(bitmap);
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.drawabletobitmap_android_examples.com.MainActivity" >

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 
 />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/imageView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="112dp"
 android:text="Click here to convert drawable image into bitmap in android" />

</RelativeLayout>

Screenshot :

Convert drawable image into bitmap in android programmatically

Click here to download Convert drawable image into bitmap in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *