1

On some devices (Nexus 9, Nexus S, ...) the camera loads a rotated picture in ImageView. I've tried to fix that with ExifInterface but no success. Does anybody have an idea on this code sample?

compiled SDK version is 25
min SDK version is 15

public void capturePhoto(String targetFilename) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}
}
Chetan Joshi
  • 5,279
  • 4
  • 24
  • 33
Bene
  • 21
  • 1
  • 1
    I encountered the same issue long back, where some devices, (Samsung and Lenovo) had orientation issues, where code worked perfectly on rest of the devices, What i did was to use [Glide](https://github.com/bumptech/glide), to place image on image view, even tried with picasso and universal image loader, But somehow Glide worked – Sanoop Surendran Aug 09 '17 at 12:34
  • try this: https://stackoverflow.com/a/8914291 – TOho Aug 09 '17 at 12:38
  • 2
    refer this https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices-on-a – Bhoomi Zalavadiya Aug 09 '17 at 12:40

1 Answers1

0

use following code:

@Override
    public void onPictureTaken(byte[] data, Camera camera) {
int rotation = getPhotoRotation();
        rotatePicture(rotation, data);
        savePicture();
        setSafeToTakePhoto(true);
    }

get rotation of device from here:

private int getPhotoRotation() {
        int rotation;
        int orientation = mOrientationListener.getRememberedNormalOrientation();
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(mCameraID, info);

        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            rotation = (info.orientation - orientation + 360) % 360;
        } else {
            rotation = (info.orientation + orientation) % 360;
        }

        return rotation;
    }
     private int getRememberedNormalOrientation() {
            rememberOrientation();
            return mRememberedNormalOrientation;
        }
     private void rememberOrientation() {
            mRememberedNormalOrientation = mCurrentNormalizedOrientation;
        }

Use above code to detect mobile orientation and save image as per device rotaion if you dont want to recognize mobile rotation set int orientation = your rotation;

Shashwat Gupta
  • 820
  • 8
  • 19