bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut) making image size bigger than original

Dany Y picture Dany Y · Apr 23, 2013 · Viewed 40.2k times · Source

I want to compress the image taken from a camera to png format to make them smaller in size, so I'm using this code:

compressedPictureFile = new File(imagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
FileOutputStream fOut = new FileOutputStream(compressedPictureFile);
boolean compressed = bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
fOut.flush();
fOut.close();

The problem is that compressedPictureFile is actually bigger that the original image (from 1 Mb to 6Mb)

What am I missing? And is this the best way to reduce the size of an image?

Thanks

Answer

Jave picture Jave · Apr 23, 2013

The images taken with the camera are most likely stored in the jpg-format, which is lossy, but is relatively good for images with lots of colors (such as photographs).

When you compress the bitmap using your method you save it as a png. The png compression is lossless, but can achieve pretty small filesizes when there are few colors (such as in certain logotypes or other graphics). When the amount of colors and complexity in a png-file increases, so will the file size (this is why the camera saves a jpg - the quality/filesize ratio is much better than png for most photographs).

So, if you want to decrease the filesize of the photographs, use jpg compression and experiment with the quality-parameter. You might also want to decrease the resolution of the images, since this will save lots of space (a file with 50% resolution will be approx. 25% in data-size).