Image keeps rotating android studio

Viewed 54

The picture keeps auto-rotating when I select an image from the gallery and the camera. I have tried the code below and I keep getting the same result. I am new to android studio so I cant find a way to correct the orientation of the image

protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK) {
        try {

            Uri selectedImage = data.getData();
            InputStream imageStream = getContentResolver().openInputStream(selectedImage);
            Bitmap selectedImages = BitmapFactory.decodeStream(imageStream);
            holdpic = getBytes(getResizedBitmaps(selectedImages, 350));
            patientPic.setImageBitmap(getResizedBitmaps(selectedImages, 350));
            patientPic.setBackgroundResource(0);
            patientPic.setMaxHeight(350);

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap image = (Bitmap) extras.get("data");
        holdpic = getBytes(getResizedBitmaps(image, 350));
        patientPic.setImageBitmap(getResizedBitmaps(image, 350));
        patientPic.setMaxHeight(350);
        patientPic.setBackgroundResource(0);
    }


} 
  public Bitmap getResizedBitmaps(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float) width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
 }

    } else {

        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}

     
     
1 Answers

I faced this issue before and tried to fix it with Exifinterface, but won't be worked, then I found a workaround, I used Glide.

Glide.with(ivImage)
     .load("file:${data[position].path}")
     .into(ivImage)
Related