Convert Drawable image to Byte Array in android

How to create/get byte array from drawable in android app programmatically through MainActivity.java programming file.

In this tutorial we are simply converting the drawable class image to byte array. So here is the complete step by step tutorial for Convert Drawable image to Byte Array in android.

android-project-download-code-button

Note: Download demo image and put inside drawable-hdpi folder.

drawable hdpi folder

Below is the demo image :

demo_img

How to Convert Drawable image to Byte Array in android.

Code for MainActivity.java file.

 package com.convertdrawableimagetobytearray_android_examples.com;

import java.io.ByteArrayOutputStream;
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 {

 Button button;
 ImageView imageview;
 Drawable drawable;
 Bitmap bitmap1, bitmap2;
 ByteArrayOutputStream bytearrayoutputstream;
 byte[] BYTE;
 
 @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);
 
 bytearrayoutputstream = new ByteArrayOutputStream();
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 drawable = getResources().getDrawable(R.drawable.demo_img);

 bitmap1 = ((BitmapDrawable)drawable).getBitmap();

 bitmap1.compress(Bitmap.CompressFormat.JPEG,70,bytearrayoutputstream);

 BYTE = bytearrayoutputstream.toByteArray();
 
 bitmap2 = BitmapFactory.decodeByteArray(BYTE,0,BYTE.length);
 
 imageview.setImageBitmap(bitmap2);
 
 }
 });
 }

}

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.convertdrawableimagetobytearray_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"
 android:src="@drawable/demo_img" />

 <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="25dp"
 android:text="Click Here To Convert Drawable image to Byte Array in android" />

</RelativeLayout>

Screenshots:

byte-array

 

Convert Drawable image to Byte Array in android

Click here to download Convert Drawable image to Byte Array in android project with source code.

Leave a Reply

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