If the photo was taken using a digital camera or smartphone, the rotation is often saved on Exif photos as part of the image file. You can read Exif metafiles using Android ExifInterface .
First create an ExifInterface :
ExifInterface exif = new ExifInterface(uri.getPath());
Then find the current rotation:
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Convert exif to degrees:
int rotationInDegrees = exifToDegrees(rotation);
Where
private static int exifToDegrees(int exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; }
Then use the actual image rotation as a reference point to rotate the image using Matrix .
Matrix matrix = new Matrix(); if (rotation != 0f) {matrix.preRotate(rotationInDegrees);}
You create a new rotated image using the Bitmap.createBitmap method, which takes a Matrix as a parameter:
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
where Matrix m contains a new rotation:
Bitmap adjustedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);
See these guides for useful source code examples: