得到的回答其旋轉到90。方向與EXIF格式的圖像一起保存。我們必須讀取每個圖像數據的方向標籤..
public static float rotationForImage(Context context, Uri uri) {
if (uri.getScheme().equals("content")) {
String[] projection = { Images.ImageColumns.ORIENTATION };
Cursor c = context.getContentResolver().query(
uri, projection, null, null, null);
if (c.moveToFirst()) {
return c.getInt(0);
}
} else if (uri.getScheme().equals("file")) {
try {
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation = (int)exifOrientationToDegrees(
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL));
return rotation;
} catch (IOException e) {
Log.e(TAG, "Error checking exif", e);
}
}
return 0f;
}
private static float exifOrientationToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
}
旋轉值可以用來校正照片的方向如下:
Matrix matrix = new Matrix();
float rotation = PhotoTaker.rotationForImage(context, uri);
if (rotation != 0f) {
matrix.preRotate(rotation);
}
Bitmap resizedBitmap = Bitmap.createBitmap(
sourceBitmap, 0, 0, width, height, matrix, true);
畫廊知道手機的確切方位同時拍照。所以它旋轉圖片。但我不明白。 – 2012-02-02 05:54:09
** [本教程可以幫助您](http://startandroiddevelopment.blogspot.in/2013/10/importing-image-from-gallery.html)** – 2013-10-24 10:15:37