图像具有不同的方向,因此在进行imageview时它会根据方向旋转.您可以从图像属性检查照片的方向.要以正确的方式设置图像,您可以使用以下代码...
int rot=getCameraPhotoOrientation(this,Uri,picturePath); if(rot!=0) bitmap=new RotateOrientation().RotateOrientationCall(bitmap,rot);
该getCameraPhotoOrientation方法: -
public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){ int rotate = 0; try { context.getContentResolver().notifyChange(imageUri, null); File imageFile = new File(imagePath); ExifInterface exif = new ExifInterface( imageFile.getAbsolutePath()); int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; } Log.d(TAG, "Exit orientation: " + orientation); } catch (Exception e) { e.printStackTrace(); } return rotate; }
添加RotateOrientation类以根据方向旋转类.
public class RotateOrientation { Bitmap RotateOrientationCall(Bitmap src,float degree) { Matrix matrix=new Matrix(); matrix.postRotate(degree); Bitmap bmOut = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); return bmOut; } }