我做了一个安卓应用程序。在我的应用中,我要截取一个图片并把它发送到服务器上。在某种设备里,截取的图片上传到服务器上的时候会旋转90度。代码如下:
Uri selectedImage = data.getData();
File imageFile = new File(selectedImage.toString());
ExifInterface exif;
try {
exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate=90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate=180;
break;
}
但是很不幸,在每个设备里我得到的orientation的值都是0,甚至图片旋转90度的那个设备上也是一样的结果。
我用这个方法解决了问题:
private int getImageOrientation(){
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageColumns, null, null, imageOrderBy);
if(cursor.moveToFirst()){
int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
rotate=orientation;
System.out.println("orientation==="+orientation);
cursor.close();
return orientation;
} else {
return 0;
}
}
程序有问题,再去看看其他的程序能不能用
Uri selectedImage = data.getData();
File imageFile = new File(selectedImage.toString());
selectedImage.toString() 这一句有问题,这里出来的结果不是图片路径, 你需要做的是得到图片文件路径