随机旋转的Android相机图像

随机旋转的Android相机图像,android,random,camera,image-rotation,Android,Random,Camera,Image Rotation,我发现了一个非常有趣的问题。在拍摄相机照片后(我将设备保持在纵向模式,不旋转),给定的照片有时会旋转,但并不总是旋转。我知道,有些设备总是提供旋转的照片,但它可以使用exif或mediastore信息进行旋转。但在这种情况下,exif和mediastore表示方向为0,但图像是旋转的。最令人沮丧的是,这完全是随机发生的。代码非常简单: Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(andr

我发现了一个非常有趣的问题。在拍摄相机照片后(我将设备保持在纵向模式,不旋转),给定的照片有时会旋转,但并不总是旋转。我知道,有些设备总是提供旋转的照片,但它可以使用exif或mediastore信息进行旋转。但在这种情况下,exif和mediastore表示方向为0,但图像是旋转的。最令人沮丧的是,这完全是随机发生的。代码非常简单:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, currentFileUri);
startActivityForResult(intent, RequestCodeCollection.CAMERA_IMAGE_CAPTURE);

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            oldImageExifInterface = new ExifInterface(currentFileUri.getPath());
        }
}
有人见过这个问题吗?在操作系统更新(4.1.1)后,我体验了Galaxy Nexus的体验。

试试这个

try {
        File f = new File(imagePath);
        ExifInterface exif = new ExifInterface(f.getPath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        int angle = 0;

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle = 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle = 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle = 270;
        }

        Matrix mat = new Matrix();
        mat.postRotate(angle);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;

        Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
                null, options);
        bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                bmp.getHeight(), mat, true);
        ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100,
                outstudentstreamOutputStream);
        imageView.setImageBitmap(correctBmp);

    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    } catch (OutOfMemoryError oom) {
        Log.w("TAG", "-- OOM Error in setting image");
    }
这里可能有一个复制品-