我实现了捕获/选择图像的功能,它在 HTC 上运行良好,但是,在三星 Galaxy Note 4(Android 版本为 5.1.1)上,它将图像旋转 90 度。以下是 2 种代码变体,但仍在轮换:
变体 1:
public void captureImageCameraOrGallery() {
Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT, null);
galleryintent.setType("image/*");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryintent);
chooser.putExtra(Intent.EXTRA_TITLE, "Select from:");
Intent[] intentArray = { cameraIntent };
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser, REQUEST_PIC);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_PIC && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
Bitmap bmp = null;
try {
if (selectedImageUri != null) {
bmp = getBitmapFromUri(selectedImageUri);
}
if (bmp == null) {
return;
}
ExifInterface ei = new ExifInterface(selectedImageUri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Log.e("Capture orientation: ", String.valueOf(orientation));
int rotateAngle = 0;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotateAngle = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateAngle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotateAngle = 270;
break;
default:
break;
}
bmp = rotateImage(bmp, rotateAngle);
mUserImage.setImageBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
}
}
变体 2:
使用PhotoPicker库编译 'me.iwf.photopicker:PhotoPicker:0.9.5@aar'
public void captureImageCameraOrGallery() {
PhotoPicker.builder()
.setPhotoCount(1)
.setShowCamera(true)
.setShowGif(true)
.setPreviewEnabled(false)
.start(this, PhotoPicker.REQUEST_CODE);
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
if (data != null) {
ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
Uri selectedImageUri = Uri.fromFile(new File(photos.get(0)));
Bitmap bmp = null;
try {
if (selectedImageUri != null) {
bmp = getBitmapFromUri(selectedImageUri);
}
if (bmp == null) {
return;
}
mUserImage.setImageBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
然而,它仍在旋转。任何帮助将不胜感激。