Loads a bitmap from a file, and applies the rotation that is specified in the EXIF data of the file.
Note: it’s often easier to use Glide instead.
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = scale;
Bitmap bm = BitmapFactory.decodeFile(path, options);
Bitmap bitmap = bm;
try{
ExifInterface exif = new ExifInterface(path);
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Matrix m = new Matrix();
if ((orientation == 3)) {
m.postRotate(180);
m.postScale((float) bm.getWidth(), (float) bm.getHeight());
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
} else if (orientation == 6) {
m.postRotate(90);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
} else if (orientation == 8) {
m.postRotate(270);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
}
} catch (Exception e) {
Log.e(TAG, "Error while rotating bitmap", e);
}
return bitmap;