I am capturing image from camera and selecting image from gallery. In samsung devices the images gets rotate after captured.
I want to rotate image to straight if they are rotated.
I tried to do it but its not working. I am getting EXIF orientation as 0 always though the image is rotated.
private void onCaptureImageResult(Intent data) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Uri tempUri = getImageUri(getApplicationContext(), thumbnail);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
ExifInterface ei = new ExifInterface(String.valueOf(finalFile));
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(finalFile));
Bitmap rotatedBitmap = null;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = bitmap;
break;
}
if(rotatedBitmap != null)
{
profile_image.setImageBitmap(rotatedBitmap);
thumbnail = rotatedBitmap;
}
}
catch (IOException ex) { }
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
true);
}
What is going wrong here? Please help.. Thank you..
I also faced the same issue once in my project and got it going by below code :
Replace
With
And tell if it works