在三星 Galaxy 设备上选择图像会旋转
Posted
技术标签:
【中文标题】在三星 Galaxy 设备上选择图像会旋转【英文标题】:Choosing Images on a Samsung Galaxy Device rotates 【发布时间】:2017-03-08 13:49:48 【问题描述】:我正在开发一个 android 项目,用户可以在其中上传图片作为个人资料图片。这一切都很好,直到某个时候(最终是三星更新)。从那时起,三星 Galaxy 设备上的图像(可能也在其他设备上),但在我的 HTC One XL 上运行良好,在我的联想瑜伽平板电脑和索尼(不知道具体是哪一个)上也能正常工作。但在 Galaxy 的 S6 和 S5 上,它会旋转图像。想法是,当用户拍摄图像时,图像被设置在“正常”视角。这意味着,它应该只是一个正方形,但当然,它是直立的。使用三星设备时,头部逆时针旋转 90 度。该代码在其他设备上完美运行。有人有同样的问题吗?任何想法?这是一些代码
// After image taken
public void onActivityResult(int requestCode, int resultCode, Intent data)
try
// data returned
if (resultCode != FragmentActivity.RESULT_CANCELED)
if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK)
// Set profile image from gallery
try
Uri selectedImage = data.getData();
photo = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
photo = MainUtils.rotateBitmap(photo, data, getActivity());
photo = MainUtils.resizeBitmapIfNeeded(photo, 800, 800);
byte[] byteArray;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byteArray = stream.toByteArray();
// Save image to parse as ParseFile and connect to the ParseUser (redacted)
MainUtils 方法:
public static Bitmap resizeBitmapIfNeeded(Bitmap image, int maxWidth, int maxHeight)
if (maxHeight > 0 && maxWidth > 0)
int wid = image.getWidth();
int hei = image.getHeight();
MainUtils.log(" resizeBitmapIfNeeded, size is " + wid + " X " + hei);
if (wid > maxWidth || hei > maxHeight)
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > 1)
finalWidth = (int) ((float) maxHeight * ratioBitmap);
else
finalHeight = (int) ((float) maxWidth / ratioBitmap);
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
wid = image.getWidth();
hei = image.getHeight();
MainUtils.log(" resizeBitmapIfNeeded, resized size is " + wid + " X " + hei);
return image;
else
return image;
else
return image;
public static Bitmap rotateBitmap(Bitmap bitmap, Intent data, Context context)
int orientation = 0;
try
Uri selectedImage = data.getData();
ExifInterface exif;
exif = new ExifInterface(MainUtils.getRealPathFromUri(context, selectedImage));
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
MainUtils.log("Orientation is " + orientation);
catch (IOException excep)
MainUtils.log("Rotate " + excep.getMessage());
Matrix matrix = new Matrix();
switch (orientation)
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.postRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.postRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.postRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(-90);
break;
default:
return bitmap;
try
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
catch (OutOfMemoryError e)
e.printStackTrace();
return null;
public static Bitmap rotateBitmapFromFile(String picturePath)
int orientation = 0;
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
try
ExifInterface exif = new ExifInterface(picturePath);
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
MainUtils.log("Orientation is " + orientation);
catch (IOException excep)
MainUtils.log("Rotate " + excep.getMessage());
Matrix matrix = new Matrix();
switch (orientation)
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.postRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.postRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.postRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(-90);
break;
default:
return bitmap;
try
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
catch (OutOfMemoryError e)
e.printStackTrace();
return null;
画廊意图
Intent intent = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
galleryRequest);
【问题讨论】:
为什么有两个 requestCode?请坚持一个。在代码开头发布使用过的意图。展示您使用mCurrentPhotoPath
所做的事情。
it isn't possible to upload a Image, directly made on the Camera
。 ?????
我要求您将问题集中在一个问题上。所以只有一个意图。还有一个 requestCode。选择相机或画廊。调整你的帖子。我会等的。
@greenapps 谢谢,我已经编辑过了。
重复问题。查看***.com/questions/13511356/…的回答
【参考方案1】:
重复问题,请参阅Android image selected from gallery Orientation is always 0 : Exif TAG
检查 MKJParekh 的答案。你必须做的: 1.) 检索位图媒体存储的方向 2.) 必要时根据方向旋转位图
【讨论】:
以上是关于在三星 Galaxy 设备上选择图像会旋转的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 设备上选择后置摄像头 - jsartoolkit5