Reducing Image Size
I am trying to reduce the size of an image that a user has selected from the Gallery before passing it to another intent. I am currently using the following code, but it doesn't s
Solution 1:
The best way is pass the image path through the intent, then degrade the image from there. You do not need to degrade image on current activity and pass the degraded image.
Follow here in order to degrade your image read here. There Image quality will be reduced with the inSampleSize. Higher the inSampleSize will cause lower the image size.
Solution 2:
To Compress an Image you can use below code.
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
publicclassImageResizer {
publicstatic String getCompressImageFile(File original, int width, int height, String filePath) {
BitmapsampledSrcBitmap= decodeFile(original, width, height);
if(sampledSrcBitmap == null) {
returnnull;
}
Bitmapbitmap= getRotatedImage(sampledSrcBitmap,original.getPath());
intbitmap_width= bitmap.getWidth();
intbitmap_height= bitmap.getHeight();
boolean success;
if(bitmap_width<width && bitmap_height <height){
success = writeToFile(bitmap, newFile(filePath),100);
}else{
bitmap = resize(bitmap, width, height);
success = writeToFile(bitmap, newFile(filePath),80);
}
bitmap.recycle();
if(success){
return filePath;
}else{
returnnull;
}
}
privatestatic Bitmap getRotatedImage(Bitmap sampledSrcBitmap,String path){
ExifInterface exif;
Matrixmatrix=newMatrix();
Bitmap bitmap=null;
try {
exif = newExifInterface(path);
intorientation= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
if (orientation == 6) {
matrix.postRotate(90);
} elseif (orientation == 3) {
matrix.postRotate(180);
} elseif (orientation == 8) {
matrix.postRotate(270);
}
bitmap = Bitmap.createBitmap(sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
publicstatic Bitmap resize(Bitmap sampledSrcBitmap, int width, int height) {
intsourceWidth= sampledSrcBitmap.getWidth();
intsourceHeight= sampledSrcBitmap.getHeight();
height = calculateHeight(sourceWidth, sourceHeight, width);
return Bitmap.createScaledBitmap(sampledSrcBitmap, width, height, true);
}
privatestaticintcalculateWidth(int originalWidth, int originalHeight, int height) {
return (int) Math.ceil(originalWidth / ((double) originalHeight/height));
}
privatestaticintcalculateHeight(int originalWidth, int originalHeight, int width) {
return (int) Math.ceil(originalHeight / ((double) originalWidth/width));
}
publicstatic Bitmap decodeFile(File bitmapFile, int reqWidth, int reqHeight){
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(bitmapFile.getAbsolutePath(), options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inPreferQualityOverSpeed = true;
return BitmapFactory.decodeFile(bitmapFile.getAbsolutePath(), options);
}
publicstaticintcalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
if(reqWidth == -1) {
reqWidth = options.outWidth;
}
if(reqHeight == -1) {
reqHeight = options.outHeight;
}
// Raw height and width of imagefinalintheight= options.outHeight;
finalintwidth= options.outWidth;
intinSampleSize=1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
publicstaticbooleanwriteToFile(Bitmap image, File file, int quality) {
ByteArrayOutputStreambytes=newByteArrayOutputStream();
image.compress(CompressFormat.JPEG, quality, bytes);
try {
FileOutputStreamfos=newFileOutputStream(file);
fos.write(bytes.toByteArray());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
returnfalse;
} catch (IOException e) {
e.printStackTrace();
returnfalse;
}
returntrue;
}
}
And to get compress Image path call getComressImagePath method and give selected gallery image path for compression. Pass the compress image path to next activity where you can use it to upload to the server.
publicstaticStringgetComressImagePath(String picturePath){
//Here 720 is max width and 1280 is max height. you can set this as per your need. Lower the resolution smaller the image size.returnImageResize.getCompressFile(newFile(picturePath), 720, 1280, getTempImagePath());
}
publicstaticStringgetTempImagePath(){
File root = newFile(Environment.getExternalStorageDirectory() + File.separator + "IMAGES");
if (!root.exists()) {
root.mkdirs();
}
File image = null;
image = newFile(root+File.separator+"fileName");
try {
image.createNewFile();
return image.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
returnnull;
}
Solution 3:
You can use this code ...
publicstatic Bitmap getThumbnailBitmap(final String path,
finalint thumbnailSize) {
Bitmap bitmap;
BitmapFactory.Optionsbounds=newBitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
bitmap = null;
}
intoriginalSize= (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Optionsopts=newBitmapFactory.Options();
if (thumbnailSize > 0) {
opts.inSampleSize = originalSize / thumbnailSize;
} else {
opts.inSampleSize = originalSize;
}
try {
bitmap = BitmapFactory.decodeFile(path, opts);
} catch (Exception ex) {
returnnull;
}
return bitmap;
}
Post a Comment for "Reducing Image Size"