I want to rotate an image file (photo) without changing file size :
- I create a bitmap with Bitmap.decodeFile
I apply the rotation :
Matrix matrix = new Matrix(); matrix.postRotate(90); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
I save bitmap into a file with :
bitmap.compress(CompressFormat.JPEG, 100, stream);
but the size of my original file has doubled !!
The image you load is encoded in a certain format (e.g. JPEG) and with a certain compression quality (where better quality equals larger file size). When you save it after rotating, you are specifying a potentially completely different format and compression quality, hence the size difference.
I don't think there is any way you can guarantee the same size unless both the input and output files are uncompressed bitmaps, but you can probably at least try to figure out what format and quality was used for the original image and use those settings when you save the file again using bitmap.compress . That should give you a file size in the same ballpark at least.
Apart from the comppression format try to create the Bitmap with the same config as the src image. I would suggest a RGB_565: http://developer.android.com/reference/android/graphics/Bitmap.Config.html
This would result in a better values as the default one.
Now if you render any think to mCanvas it will be saved in the bitmap:
Now mBitmap should have all you need (and the size).