Convert Bitmap image to Byte Array in android example

How to change drawable resource folder image to byte array programmatically on button click.

In this tutorial we are converting bitmap image to byte array with the use of InputStream, Bitmap, ByteArrayOutputStream, and byte[ ]. So here is the complete step by step tutorial for Convert Bitmap image to Byte Array in android example.

android-project-download-code-button

Note: Please copy the demo image inside drawable-hdpi folder.

drawable hdpi folder

Below is the demo image :

demo_img

How to Convert Bitmap image to Byte Array in android example.

Code for MainActivity.java file.

 package com.convertbitmapimagetobytearray_android_examples.com;

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

 Button button;
 ImageView imageview;
 InputStream inputstream;
 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
 
 
 inputstream = MainActivity.this.getResources().openRawResource(R.drawable.demo_img);
 
 bitmap1 = BitmapFactory.decodeStream(inputstream);

 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.convertbitmapimagetobytearray_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="38dp"
 android:text="Click here to Convert Above Bitmap image to Byte Array in android" />

</RelativeLayout>

Screenshots:

byte-array-1

 

Convert Bitmap image to Byte Array in android example

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

Leave a Reply

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