The cropAndCompressPhotoFile method crops, rescales and saves a photo with compression.
I tried to delete this method and got this problem with less reproducibility (about 2 of 15, it was 10 of 10 before).
After I deleted EXIF overriding lines too the problem probably disappeared, I tried about 20 times.
Here is my cropAndCompressPhotoFile method code:
private void cropAndCompressPhotoFile(File photoFile) throws IOException {
try {
Bitmap photoBitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
if (photoBitmap != null) {
int sourceWidth = photoBitmap.getWidth();
int sourceHeight = photoBitmap.getHeight();
int scaledWidth = Math.min(sourceHeight, sourceWidth);
int scaledHeight = (sourceHeight > sourceWidth)? sourceHeight - ( sourceHeight - sourceWidth) : sourceHeight;
int scaledX = (sourceWidth - sourceHeight) / 2;
scaledX = Math.max(scaledX, 0);
int scaledY = (sourceHeight - sourceWidth) / 2;
scaledY = Math.max(scaledY, 0);
photoBitmap = Bitmap.createBitmap(photoBitmap, scaledX, scaledY, scaledWidth, scaledHeight);
photoBitmap = Bitmap.createScaledBitmap(photoBitmap,
PHOTO_WIDTH,
PHOTO_HEIGHT,
false);
FileOutputStream fileOutputStream = new FileOutputStream(photoFile, false);
photoBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (IOException e) {
Log.e(TAG, "Compress photo error: " + e.getLocalizedMessage());
e.printStackTrace();