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:
<resources>
<string name="app_name">CompressImage</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Image Compressor</string>
</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:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="30dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="80dp"/>
<ImageView
android:layout_height="300dp"
android:layout_width="300dp"
android:id="@+id/image"
android:layout_marginLeft="40dp"
android:layout_marginTop="80dp"
android:src="@drawable/compress" />
</LinearLayout>
The layout looks like:
Step 3:
Open "activity_main" and add the following to it:
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose image"
android:textSize="30dp"
android:layout_marginLeft="50dp"/>
<ImageView
android:id="@+id/im1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="@drawable/camera"
android:onClick="compress"/>
<ImageView
android:id="@+id/im2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="@drawable/im2"
android:onClick="compress"
android:layout_marginTop="20dp"/>
</LinearLayout>
The layout looks like:
Step 4:
Right-click on the package then select "New" -> "Java class". Name it Splash_screen and add the following code to it:
package com.chhavi.compressimage;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
public class Splash_screen extends Activity {
private Thread mSplashThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
final Splash_screen sPlashScreen = this;
mSplashThread = new Thread(){
@Override
public void run(){
try {
synchronized(this){
wait(5000);
}
}
catch(InterruptedException ex){
}
finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}
}
Step 5:
Open "MainActivity" and add the following code to it:
package com.chhavi.compressimage;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
public class MainActivity extends Activity {
ImageView im;
Bitmap originalImage;
int width;
int height;
int newWidth = 200;
int newHeight = 200;
Matrix matrix;
Bitmap resizedBitmap;
float scaleWidth ;
float scaleHeight;
ByteArrayOutputStream outputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void compress(View v)
{
setContentView(R.layout.activity_main);
switch (v.getId())
{
case R.id.im1:
originalImage = BitmapFactory.decodeResource(getResources(), R.drawable.camera);
width = originalImage.getWidth();
Log.i("Old width................", width + "");
height = originalImage.getHeight();
Log.i("Old height................", height + "");
matrix = new Matrix();
scaleWidth = ((float) newWidth) / width;
scaleHeight = ((float) newHeight) / height;
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(45);
resizedBitmap = Bitmap.createBitmap(originalImage, 0, 0, width, height, matrix, true);
outputStream = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
im=(ImageView)findViewById(R.id.im1);
im.setImageBitmap(resizedBitmap);
width = resizedBitmap.getWidth();
Log.i("new width................", width + "");
height = resizedBitmap.getHeight();
Log.i("new height................", height + "");
break;
case R.id.im2:
originalImage = BitmapFactory.decodeResource(getResources(),R.drawable.im2);
width = originalImage.getWidth();
Log.i("Old width................", width + "");
height = originalImage.getHeight();
Log.i("Old height................", height + "");
matrix = new Matrix();
scaleWidth = ((float) newWidth) / width;
scaleHeight = ((float) newHeight) / height;
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(-45);
Bitmap resizedBitmap = Bitmap.createBitmap(originalImage, 0, 0, width, height, matrix, true);
im=(ImageView)findViewById(R.id.im2);
outputStream = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
im.setImageBitmap(resizedBitmap);
width = resizedBitmap.getWidth();
Log.i("new width................", width + "");
height = resizedBitmap.getHeight();
Log.i("new height................", height + "");
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
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:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chhavi.compressimage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.chhavi.compressimage.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Splash_screen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Output snapshots.
First the splash screen appears.
After some time:
Clicking on the first image will resize and compress the image.
Clicking on the second image will resize and compress the second image
Thank you