Miui 10.2.2 or above photo rotation problem and solution on Mi phones

Problem Description

  • The problem that occurs on Xiaomi phones with miui10.2.2 or above: When calling the phone album to select photos taken with the phone, the pictures will rotate after the photos are compressed.

Solution:

  • Then get the angle of rotation, and then rotate the picture back.

    1. Get the photo rotation angle, the code is as follows:
     public static int readPicDegree(String fileName) {
        int rotate = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(fileName);
            int result = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_UNDEFINED);
            switch (result) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotate = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotate = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotate = 270;
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
        }
        return rotate;
    }
    
    1. Rotate the picture, the code is as follows:
      public static Bitmap rotatePic(String fileName) {
        Bitmap bitmap = BitmapFactory.decodeFile(fileName);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.postRotate(readPicDegree(fileName));
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        return bitmap;
    }
    

    Some mobile phones also rotate after taking pictures, you can also use this method for processing

Guess you like

Origin blog.csdn.net/genmenu/article/details/87460754