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. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.MotionEvent;
  6. public class Splash_screen extends Activity {
  7. private Thread mSplashThread;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.splash_layout);
  12. final Splash_screen sPlashScreen = this;
  13. mSplashThread = new Thread(){
  14. @Override
  15. public void run(){
  16. try {
  17. synchronized(this){
  18. wait(5000);
  19. }
  20. }
  21. catch(InterruptedException ex){
  22. }
  23. finish();
  24. Intent intent = new Intent();
  25. intent.setClass(sPlashScreen, MainActivity.class);
  26. startActivity(intent);
  27. }
  28. };
  29. mSplashThread.start();
  30. }
  31. @Override
  32. public boolean onTouchEvent(MotionEvent evt)
  33. {
  34. if(evt.getAction() == MotionEvent.ACTION_DOWN)
  35. {
  36. synchronized(mSplashThread){
  37. mSplashThread.notifyAll();
  38. }
  39. }
  40. return true;
  41. }
  42. }
Step 5
Open "MainActivity" and add the following code to it:
  1. package com.chhavi.compressimage;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.Matrix;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.os.Bundle;
  7. import android.app.Activity;
  8. import android.util.Log;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.widget.EditText;
  12. import android.widget.ImageView;
  13. import java.io.ByteArrayOutputStream;
  14. public class MainActivity extends Activity {
  15. ImageView im;
  16. Bitmap originalImage;
  17. int width;
  18. int height;
  19. int newWidth = 200;
  20. int newHeight = 200;
  21. Matrix matrix;
  22. Bitmap resizedBitmap;
  23. float scaleWidth ;
  24. float scaleHeight;
  25. ByteArrayOutputStream outputStream;
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. }
  31. public void compress(View v)
  32. {
  33. setContentView(R.layout.activity_main);
  34. switch (v.getId())
  35. {
  36. case R.id.im1:
  37. originalImage = BitmapFactory.decodeResource(getResources(), R.drawable.camera);
  38. width = originalImage.getWidth();
  39. Log.i("Old width................", width + "");
  40. height = originalImage.getHeight();
  41. Log.i("Old height................", height + "");
  42. matrix = new Matrix();
  43. scaleWidth = ((float) newWidth) / width;
  44. scaleHeight = ((float) newHeight) / height;
  45. matrix.postScale(scaleWidth, scaleHeight);
  46. matrix.postRotate(45);
  47. resizedBitmap = Bitmap.createBitmap(originalImage, 0, 0, width, height, matrix, true);
  48. outputStream = new ByteArrayOutputStream();
  49. resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
  50. im=(ImageView)findViewById(R.id.im1);
  51. im.setImageBitmap(resizedBitmap);
  52. width = resizedBitmap.getWidth();
  53. Log.i("new width................", width + "");
  54. height = resizedBitmap.getHeight();
  55. Log.i("new height................", height + "");
  56. break;
  57. case R.id.im2:
  58. originalImage = BitmapFactory.decodeResource(getResources(),R.drawable.im2);
  59. width = originalImage.getWidth();
  60. Log.i("Old width................", width + "");
  61. height = originalImage.getHeight();
  62. Log.i("Old height................", height + "");
  63. matrix = new Matrix();
  64. scaleWidth = ((float) newWidth) / width;
  65. scaleHeight = ((float) newHeight) / height;
  66. matrix.postScale(scaleWidth, scaleHeight);
  67. matrix.postRotate(-45);
  68. Bitmap resizedBitmap = Bitmap.createBitmap(originalImage, 0, 0, width, height, matrix, true);
  69. im=(ImageView)findViewById(R.id.im2);
  70. outputStream = new ByteArrayOutputStream();
  71. resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
  72. im.setImageBitmap(resizedBitmap);
  73. width = resizedBitmap.getWidth();
  74. Log.i("new width................", width + "");
  75. height = resizedBitmap.getHeight();
  76. Log.i("new height................", height + "");
  77. break;
  78. }
  79. }
  80. @Override
  81. public boolean onCreateOptionsMenu(Menu menu) {
  82. // Inflate the menu; this adds items to the action bar if it is present.
  83. getMenuInflater().inflate(R.menu.main, menu);
  84. return true;
  85. }
  86. }
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. <uses-sdk
  6. android:minSdkVersion="7"
  7. android:targetSdkVersion="16" />
  8. <application
  9. android:allowBackup="true"
  10. android:icon="@drawable/ic_launcher"
  11. android:label="@string/app_name"
  12. android:theme="@style/AppTheme" >
  13. <activity
  14. android:name="com.chhavi.compressimage.MainActivity"
  15. android:label="@string/app_name" >
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18. <category android:name="android.intent.category.DEFAULT" />
  19. </intent-filter>
  20. </activity>
  21. <activity android:name=".Splash_screen">
  22. <intent-filter>
  23. <action android:name="android.intent.action.MAIN"/>
  24. <category android:name="android.intent.category.LAUNCHER"/>
  25. </intent-filter>
  26. </activity>
  27. </application>
  28. </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