Re-size and Compress Image in Android Studio

Introduction

 
In this article, you will learn how to resize and compress an image in Android. Resizing and compressing an image is sometimes very useful.
 
Step 1
 
"string.xml" used in this project is:
  1. <resources>  
  2.     <string name="app_name">CompressImage</string>  
  3.     <string name="action_settings">Settings</string>  
  4.     <string name="hello_world">Image Compressor</string>  
  5. </resources> 
Step 2
 
Right-click on "layout" then select "New" -> "Layout resource file". Name this file as "splash_layout" and add the following code to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.               android:orientation="vertical"  
  3.               android:layout_width="match_parent"  
  4.               android:layout_height="match_parent">  
  5.     <TextView  
  6.             android:layout_width="fill_parent"  
  7.             android:layout_height="wrap_content"  
  8.             android:text="@string/hello_world"  
  9.             android:textSize="30dp"  
  10.             android:layout_marginLeft="60dp"  
  11.             android:layout_marginTop="80dp"/>  
  12.     <ImageView  
  13.         android:layout_height="300dp"  
  14.         android:layout_width="300dp"  
  15.         android:id="@+id/image"  
  16.         android:layout_marginLeft="40dp"  
  17.         android:layout_marginTop="80dp"  
  18.         android:src="@drawable/compress" />  
  19. </LinearLayout>  
The layout  looks like:
 
im1.jpg
 
Step 3
 
Open "activity_main" and add the following to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:orientation="vertical">  
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="Choose image"  
  15.         android:textSize="30dp"  
  16.         android:layout_marginLeft="50dp"/>  
  17.     <ImageView  
  18.         android:id="@+id/im1"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_width="fill_parent"  
  21.         android:src="@drawable/camera"  
  22.         android:onClick="compress"/>  
  23.     <ImageView  
  24.             android:id="@+id/im2"  
  25.             android:layout_height="wrap_content"  
  26.             android:layout_width="fill_parent"  
  27.             android:src="@drawable/im2"  
  28.             android:onClick="compress"  
  29.             android:layout_marginTop="20dp"/>  
  30. </LinearLayout>  
The layout looks like:
 
im2.jpg
 
Step 4
 
Right-click on the package then select "New" -> "Java class". Name it Splash_screen and add the following code to it:
  1. package com.chhavi.compressimage;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.MotionEvent;  
  7.    
  8. public class Splash_screen extends Activity {  
  9.     private Thread mSplashThread;  
  10.    
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.splash_layout);  
  15.         final Splash_screen sPlashScreen = this;  
  16.    
  17.             mSplashThread =  new Thread(){  
  18.             @Override  
  19.             public void run(){  
  20.                 try {  
  21.                     synchronized(this){  
  22.    
  23.                         wait(5000);  
  24.                     }  
  25.                 }  
  26.                 catch(InterruptedException ex){  
  27.                 }  
  28.    
  29.                 finish();  
  30.    
  31.                 Intent intent = new Intent();  
  32.                 intent.setClass(sPlashScreen, MainActivity.class);  
  33.                 startActivity(intent);  
  34.             }  
  35.         };   
  36.         mSplashThread.start();  
  37.     }  
  38.    
  39.     @Override  
  40.    
  41.     public boolean onTouchEvent(MotionEvent evt)  
  42.     {  
  43.         if(evt.getAction() == MotionEvent.ACTION_DOWN)  
  44.         {  
  45.             synchronized(mSplashThread){  
  46.                 mSplashThread.notifyAll();  
  47.             }  
  48.         }  
  49.         return true;  
  50.     }  
Step 5
 
Open "MainActivity" and add the following code to it:
  1. package com.chhavi.compressimage;  
  2.    
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.graphics.Matrix;  
  6. import android.graphics.drawable.BitmapDrawable;  
  7. import android.os.Bundle;  
  8. import android.app.Activity;  
  9. import android.util.Log;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.widget.EditText;  
  13. import android.widget.ImageView;  
  14. import java.io.ByteArrayOutputStream;  
  15.    
  16. public class MainActivity extends Activity {  
  17.    
  18.     ImageView im;  
  19.     Bitmap originalImage;  
  20.     int width;  
  21.     int height;  
  22.     int newWidth = 200;  
  23.     int newHeight = 200;  
  24.     Matrix matrix;  
  25.     Bitmap resizedBitmap;  
  26.     float scaleWidth ;  
  27.     float scaleHeight;  
  28.     ByteArrayOutputStream outputStream;  
  29.    
  30.     @Override  
  31.     protected void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.activity_main);  
  34.     }  
  35.    
  36.     public void compress(View v)  
  37.     {  
  38.         setContentView(R.layout.activity_main);  
  39.         switch (v.getId())  
  40.         {  
  41.             case R.id.im1:  
  42.    
  43.                 originalImage = BitmapFactory.decodeResource(getResources(), R.drawable.camera);  
  44.                 width = originalImage.getWidth();  
  45.                 Log.i("Old width................", width + "");  
  46.                 height = originalImage.getHeight();  
  47.                 Log.i("Old height................", height + "");  
  48.    
  49.                 matrix = new Matrix();   
  50.                 scaleWidth = ((float) newWidth) / width;  
  51.                 scaleHeight = ((float) newHeight) / height;   
  52.                 matrix.postScale(scaleWidth, scaleHeight);  
  53.                 matrix.postRotate(45);  
  54.   
  55.                 resizedBitmap = Bitmap.createBitmap(originalImage, 00, width, height, matrix, true);  
  56.                 outputStream = new ByteArrayOutputStream();  
  57.                 resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);  
  58.                 im=(ImageView)findViewById(R.id.im1);  
  59.                 im.setImageBitmap(resizedBitmap);  
  60.                 width = resizedBitmap.getWidth();  
  61.                 Log.i("new width................", width + "");  
  62.                 height = resizedBitmap.getHeight();  
  63.                 Log.i("new height................", height + "");  
  64.    
  65.             break;  
  66.    
  67.             case R.id.im2:  
  68.    
  69.                  originalImage = BitmapFactory.decodeResource(getResources(),R.drawable.im2);  
  70.                  width = originalImage.getWidth();  
  71.                  Log.i("Old width................", width + "");  
  72.                  height = originalImage.getHeight();  
  73.                  Log.i("Old height................", height + "");  
  74.    
  75.                  matrix = new Matrix();  
  76.                  scaleWidth = ((float) newWidth) / width;  
  77.                  scaleHeight = ((float) newHeight) / height;  
  78.                  matrix.postScale(scaleWidth, scaleHeight);  
  79.                  matrix.postRotate(-45);  
  80.                  Bitmap resizedBitmap = Bitmap.createBitmap(originalImage, 00, width, height, matrix, true);  
  81.                  im=(ImageView)findViewById(R.id.im2);  
  82.    
  83.                  outputStream = new ByteArrayOutputStream();  
  84.                  resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);  
  85.                  im.setImageBitmap(resizedBitmap);  
  86.                  width = resizedBitmap.getWidth();  
  87.                  Log.i("new width................", width + "");  
  88.                  height = resizedBitmap.getHeight();  
  89.                  Log.i("new height................", height + "");  
  90.    
  91.                  break;  
  92.         }  
  93.     }  
  94.    
  95.     @Override  
  96.     public boolean onCreateOptionsMenu(Menu menu) {  
  97.         // Inflate the menu; this adds items to the action bar if it is present.  
  98.         getMenuInflater().inflate(R.menu.main, menu);  
  99.         return true;  
  100.     }     
In the code above, the "compress" function is used to compress the bitmap. "createBitmap" resizes the bitmap.
 
Step 6
 
Open "AndroidManifest" and do the following changes in it:
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.chhavi.compressimage"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.    
  6.     <uses-sdk  
  7.         android:minSdkVersion="7"  
  8.         android:targetSdkVersion="16" />  
  9.    
  10.     <application  
  11.         android:allowBackup="true"  
  12.         android:icon="@drawable/ic_launcher"  
  13.         android:label="@string/app_name"  
  14.         android:theme="@style/AppTheme" >  
  15.         <activity  
  16.             android:name="com.chhavi.compressimage.MainActivity"  
  17.             android:label="@string/app_name" >  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.                 <category android:name="android.intent.category.DEFAULT" />  
  21.             </intent-filter>  
  22.         </activity>  
  23.    
  24.         <activity android:name=".Splash_screen">  
  25.             <intent-filter>  
  26.                 <action android:name="android.intent.action.MAIN"/>  
  27.                 <category android:name="android.intent.category.LAUNCHER"/>  
  28.             </intent-filter>  
  29.         </activity>  
  30.        
  31.     </application>  
  32.    
  33. </manifest> 
Output snapshots.
 
First, the splash screen appears.
 
im3.jpg
 
After some time:
 
im4.jpg
 
Clicking on the first image will resize and compress the image.
 
im5.jpg
 
Clicking on the second image will resize and compress the second image
 
im6.jpg
 
Thank you