在我的应用程序中,我可以选择照片(纵向/横向),并在此代码的帮助下以原始方向显示照片:
public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath) {
int rotate = 0;
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
try {
ExifInterface exif;
exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
System.out.println("ExifInterface.TAG_ORIENTATION: "+ ExifInterface.TAG_ORIENTATION
+ "\nExifInterface.ORIENTATION_NORMAL: " + 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;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rotate;
}
然后我将我的Sony手机更新为4.4.2,现在它的返回值始终为0。 在我的System.out.println中,我检查orientation_normal,它始终为1(横向/纵向)。 这是我称之为方法的地方:
selectedImageUri = data.getData();
screenShotUri = selectedImageUri;
selectedImagePath = getPath(selectedImageUri);
String fullPath = selectedImagePath;
if (fullPath==null){
return;
}
int index = fullPath.lastIndexOf("/");
String fileNameUpperCase = fullPath.substring(index + 1);
fileName = fileNameUpperCase.toLowerCase();
pathTextView.setText("" + selectedImagePath);
imageView.setVisibility(View.VISIBLE);
imageView.setImageURI(selectedImageUri);
int rotate = controller.getCameraPhotoOrientation(this,
selectedImageUri, selectedImagePath);
这是我旋转照片的地方:
if (rotate == 90) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix, true);
imageView.setRotation(90f);
} else if (rotate == 180) {
Matrix matrix = new Matrix();
matrix.postRotate(180);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix, true);
imageView.setRotation(180f);
} else if (rotate == 270) {
Matrix matrix = new Matrix();
matrix.postRotate(270);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix, true);
imageView.setRotation(270f);
}