使用下面类
String path="path of your image";
imageview.setImageBitmap(ExifUtil.rotateBitmap(path, BitmapFactory.decodeFile(path)));
ExifUtil.java
public class ExifUtil {
/**
* @see http://sylvana.net/jpegcrop/exif_orientation.html
*/
public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
try {
int orientation = getExifOrientation(src);
if (orientation == 1) {
return bitmap;
}
Matrix matrix = new Matrix();
switch (orientation) {
case 2:
matrix.setScale(-1, 1);
break;
case 3:
matrix.setRotate(180);
break;
case 4:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case 5:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case 6:
matrix.setRotate(90);
break;
case 7:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case 8:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
private static int getExifOrientation(String src) throws IOException {
int orientation = 1;
try {
/**
* if your are targeting only api level >= 5
* ExifInterface exif = new ExifInterface(src);
* orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
*/
if (Build.VERSION.SDK_INT >= 5) {
Class<?> exifClass = Class.forName("android.media.ExifInterface");
Constructor<?> exifConstructor = exifClass.getConstructor(new Class[] { String.class });
Object exifInstance = exifConstructor.newInstance(new Object[] { src });
Method getAttributeInt = exifClass.getMethod("getAttributeInt", new Class[] { String.class, int.class });
Field tagOrientationField = exifClass.getField("TAG_ORIENTATION");
String tagOrientation = (String) tagOrientationField.get(null);
orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[] { tagOrientation, 1});
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return orientation;
}
}
至于单证云:'getAttributeInt' “返回指定标签的整数值,如果图片文件中没有这样的标签或者该值不能被解析为整数,返回defaultValue。“,这意味着标签没有被定义,并且ExifInterface.ORIENTATION_UNDEFINED(0)被返回,或者你的开关没有处理ORIENTATION_(某些)情况。添加一个默认子句,然后记录'orientation'返回。 – Bonatti
好的非常感谢..会做到这一点..我已经检查方向的价值,然后在开关它说0 .. – Keshav1234
还请注意,这个“问题”是旧的,[自2012年以来](http://stackoverflow.com/问题/ 13245556/exif-orientation-tag-value-always-0-for-image-taken-portrait-camera-app-and)知道Samsung不会正确保存Exif数据。 [即使是Exif标准](https://en.wikipedia.org/wiki/Exchangeable_image_file_format#Problems)也会产生不兼容情况。由于Samsung不尊重Exif界面,您可以自己编辑该设置(如果您要求Camera Intent,请使用设备方向)或检查位图高度/宽度,然后相应地翻转图像。 – Bonatti