Compress Bitmap image in android and Reduce image size programmatically

How to create small size image from original drawable bitmap image + decrease image size up to 50% in android.

Image compression is very important feature for low speed internet connection devices because when application user usages 2G internet connection or slow WiFi then some times image loads slowly because of slow internet. Android’s bitmap calls  compress() method gives us the ability to compress any image and decrease image size so it loads more fast. So here is the complete step by step tutorial for Compress Bitmap image in android and Reduce image size programmatically.

android-project-download-code-button

Note : Download demo image from below and copy inside drawable-hdpi folder.

drawable hdpi folder

Below is the demo image :

demo_image

How to Compress Bitmap image in android and Reduce image size programmatically.

Code for MainActivity.java file.

 package com.compressbitmapimage_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;
import android.widget.Toast;

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);
 
 bytearrayoutputstream = new ByteArrayOutputStream();
 
 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
 
 drawable = getResources().getDrawable(R.drawable.demo_image);
 
 bitmap1 = ((BitmapDrawable)drawable).getBitmap();

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

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

 imageview.setImageBitmap(bitmap2);
 
 Toast.makeText(MainActivity.this, "Image Compressed Successfully", Toast.LENGTH_LONG).show();

 }
 });
 }

}

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.compressbitmapimage_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_image" />

 <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="23dp"
 android:text="Click here to Compress Bitmap image in android and Reduce image size programmatically" />

</RelativeLayout>

Screenshot:

Compress Bitmap image in android and Reduce image size programmatically

Click here to download Compress Bitmap image in android and Reduce image size programmatically project with source code.

Leave a Reply

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