'Why Images are rotated 90 degree in ImageView?

Why images are rotated 90 degree in imageView?? and how to fix it??

All images in gallery are not rotated 90 degree in imageView.

I don't know why some images are rotated 90 degree in imageView.

@Override
protected void onActivityResult(int requestCode , int resultCode , Intent data){
    if(requestCode == 100){
        if(resultCode == Activity.RESULT_OK){
            try{
                ImageView imageView = (ImageView)View.inflate(this, R.layout.imagelayout, null);
                Uri imgUri = data.getData();

             Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
                int height = bitmap.getHeight();
                int width = bitmap.getWidth();
                int newHeight = height;
                int newWidth = width;
                float rate = 0.0f;

                if(width > height ){
                    if(imageFlipper.getWidth() < width ){
                        rate = imageFlipper.getWidth() / (float) width ;
                        newHeight = (int) (height * rate);
                        newWidth = imageFlipper.getWidth();
                    }
                }else{
                    if(imageFlipper.getHeight() < height ){
                        rate = imageFlipper.getHeight() / (float) height ;
                        newWidth = (int) (width * rate);
                        newHeight = imageFlipper.getHeight();
                    }
                }


              Bitmap reSize = Bitmap.createScaledBitmap(bitmap , newWidth , newHeight,true);

              imageView.setImageBitmap(reSize);

              //imageView.setImageURI(imgUri);
                imageFlipper.addView(imageView);
                imageFlipper.setDisplayedChild(imageFlipper.getChildCount() - 1);

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}


Solution 1:[1]

In some devices, when the camera is launched the orientation would change. In one of my apps, I also faced this issue. To handle this we need to find the orientation and rotate the picture accordingly.

// capture image orientation

    public int getCameraPhotoOrientation(Context context, Uri imageUri,
            String imagePath) {
        int rotate = 0;
        try {
            context.getContentResolver().notifyChange(imageUri, null);
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            }

            Log.i("RotateImage", "Exif orientation: " + orientation);
            Log.i("RotateImage", "Rotate value: " + rotate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rotate;
    }

We need to use this integer returned to set the angle for Imageview.

int rotateImage = getCameraPhotoOrientation(this, uriLargeImage,
                    mediaFile.getAbsolutePath());

articleview.setRotation(rotateImage); // articleview is ImageView

So basically, please find the orientation of the photo as and when it is taken. This is in ExifInterface. Use this information to rotate.

Hope this helps. All the best.

Solution 2:[2]

You must set the rotation in the imageview with which the photo was taken:

 imageView.setRotation(getCameraPhotoOrientation(filepath));
 imageView.setImageURI( Uri.fromFile(filepath));



 public static int getCameraPhotoOrientation(String imagePath) {
    int rotate = 0;
    try {
        ExifInterface exif  = null;
        try {
            exif = new ExifInterface(imagePath);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, 0);
        switch (orientation) {

            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;

            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 90;
                break;
            default:
                rotate = 0;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

Solution 3:[3]

As mentioned before it does it automatically. The way I fixed this, was by using this ImageCropper, which counter-rotates it automatically, which is amazing.

Click here: https://github.com/ArthurHub/Android-Image-Cropper

So not only do you get to crop and rotate your image, but it fixes the problem too!!

Solution 4:[4]

/** * Rotate a bitmap clockwise and anticlockwise */

    btn_clock = (Button) findViewById(R.id.btn_clockWise);
    btn_clock.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Matrix mMatrix = new Matrix();
            Matrix mat=img.getImageMatrix();
            mMatrix.set(mat);
            mMatrix.setRotate(90);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), mMatrix, false);
            img.setImageBitmap(bitmap);
        }
    });


    btn_antiClock = (Button) findViewById(R.id.btn_AnticlockWise);
    btn_antiClock.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Matrix mMatrix = new Matrix();
            Matrix mat=img.getImageMatrix();
            mMatrix.set(mat);
            mMatrix.setRotate(-90);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), mMatrix, false);
            img.setImageBitmap(bitmap);
        }
    });

Solution 5:[5]

Kotlin answer

use:

    implementation 'androidx.exifinterface:exifinterface:VERSION'

Get newest release here: https://developer.android.com/jetpack/androidx/releases/exifinterface

Function:

private fun adjustRotation(file: File): Bitmap{
    lateinit var rotatedBitmap: Bitmap
    val bitmap = BitmapFactory.decodeFile(file.absolutePath)
    val exifInterface = ExifInterface(file)
    val orientation: Int = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)

    fun rotateBitmap(source: Bitmap, angle: Int): Bitmap{
        val matrix = Matrix()
        matrix.postRotate(angle.toFloat())
        return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
    }

    when (orientation){
        ExifInterface.ORIENTATION_ROTATE_90 ->
            rotatedBitmap = rotateBitmap(bitmap, 90)
        ExifInterface.ORIENTATION_ROTATE_180 ->
            rotatedBitmap = rotateBitmap(bitmap, 180)
        ExifInterface.ORIENTATION_ROTATE_270 ->
            rotatedBitmap = rotateBitmap(bitmap, 270)
        ExifInterface.ORIENTATION_NORMAL ->
            rotatedBitmap = bitmap
        else -> rotatedBitmap = bitmap
    }
    return rotatedBitmap
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Natarajan Raman
Solution 2 Vins
Solution 3 Community
Solution 4 Abd Nezar
Solution 5 BlazeCodeDev