0

I've found a very interesting problem. After taking a camera picture (I hold the device in portrait mode, and not rotating), the given photo is sometimes rotated, but not always. I know, some device give always rotated photo, but it can be rotated with exif or mediastore information. But in this case, exif and mediastore says orientation is 0, but the image is rotated. The most frustrating thing this comes totally randomly. The code is very simple:

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());
        }
}

Have anybody seen this problem? I experienced on Galaxy Nexus after OS update (4.1.1)

ba-res-ba
  • 150
  • 1
  • 9
  • Possible duplicate of [why image captured using camera intent gets rotated on some devices in android](http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android) – Shirish Herwade Mar 11 '16 at 13:50
  • Answere present here - http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android – Shirish Herwade Mar 11 '16 at 13:51

1 Answers1

0

Try this.

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");
    }
Ameer
  • 2,709
  • 1
  • 28
  • 44