Reputation: 3102
I'm unable to get the orientation when selecting an image from my library. If I go to image details, I can see the image orientation is set to 90 degrees. However, my orientation is always 0.
String[] orientationColumn = { MediaStore.Images.ImageColumns.ORIENTATION };
Cursor cur = managedQuery(data.getData(), orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Using ExitInterface:
ExifInterface exif = new ExifInterface(data.getData().getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION);
Both ways return 0. I launch the select from library activity like so:
protected void selectFromLibrary() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent,
REQUEST_SELECT_IMAGE_FILE);
}
This is on an LG G2 running 4.4.2
Upvotes: 2
Views: 680
Reputation: 1753
I've come across couple of solutions (like here Images taken with ACTION_IMAGE_CAPTURE always returns 1 for ExifInterface.TAG_ORIENTATION on some newer devices), but none of them where working for me.
What finally saved me was this piece of code: http://androidxref.com/4.0.4/xref/packages/apps/Gallery2/src/com/android/gallery3d/data/Exif.java
So, at the beginning I am trying to get orientation using default exif methods. When it fails, I am releasing the beast. My full code:
protected Matrix getRotationMatrix(String path, String mimeType, Context ctx, Uri imgUri)
{
Matrix mtx = new Matrix();
try {
ExifInterface exif = new ExifInterface(path);
if (mimeType.contains("jpeg") && exif != null) {
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
if (orientation != ExifInterface.ORIENTATION_UNDEFINED) {
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
break;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
mtx.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
mtx.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
mtx.setRotate(180);
mtx.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
mtx.setRotate(90);
mtx.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
mtx.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
mtx.setRotate(-90);
mtx.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
mtx.setRotate(-90);
break;
}
}
else
{
if (ctx != null && imgUri != null)
{
Cursor cursor = ctx.getContentResolver().query(imgUri,
new String[]{MediaStore.Images.ImageColumns.ORIENTATION},
null, null, null);
try {
if (cursor.moveToFirst()) {
orientation = cursor.getInt(0);
if (orientation != ExifInterface.ORIENTATION_UNDEFINED)
mtx.postRotate(cursor.getInt(0));
else {
// last try...
mtx.postRotate( Exif.getOrientation(ctx.getContentResolver().openInputStream(imgUri)));
}
}
} finally {
cursor.close();
}
}
}
}
}
catch(Exception ex)
{
return mtx;
}
return mtx;
}
Then I use this matrix inside Bitmap.createBitmap
method to get image rotated in right way.
Upvotes: 5