How to Convert Bitmap image to drawable in android

How to convert and show bitmap image to drawable inside ImageView on button click.

In this tutorial we are simply converting the bitmap image to drawable. So here is the complete step by step tutorial for How to Convert Bitmap image to drawable in android.

android-project-download-code-button

Note: Please upload image inside drawable-hdpi folder.

drawable hdpi folder

This is demo image.

demo_img

How to Convert Bitmap image to drawable in android.

Code for MainActivity.java file.

 package com.bitmaptodrawable_android_examples.com;

import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

 ImageView imageview;
 Button button;
 InputStream inputstream;
 Bitmap bitmap;
 Drawable drawable;
 Integer[] ImageName = {R.drawable.demo_img};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 imageview = (ImageView)findViewById(R.id.imageView1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 inputstream = MainActivity.this.getResources().openRawResource(R.drawable.demo_img);
 
 bitmap = BitmapFactory.decodeStream(inputstream);
 
 drawable = new BitmapDrawable(getResources(),bitmap);
 
 imageview.setImageDrawable(drawable);
 
 
 }
 });
 }

}

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.bitmaptodrawable_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="19dp"
 android:text="Click here to Show and Convert Bitmap image to drawable in android from drawable folder" />

</RelativeLayout>

Screenshot:

How to Convert Bitmap image to drawable in android

Click here to download How to Convert Bitmap image to drawable in android project with source code.

Leave a Reply

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