使用下面類
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