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 seem to be working:
private Bitmap decodeFile(File f) throws IOException {
Bitmap b = null;
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = null;
fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int)Math.pow(2, (int) Math.ceil(Math.log(IMAGE_MAX_SIZE /
(double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
return b;
}
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.
You can use this code ...
To Compress an Image you can use below code.
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.