Perfect solution to the problem of Android Samsung mobile phone selecting photo rotation from gallery

  • 2021-07-03 00:50:32
  • OfStack

Recently, I solved a problem that has been causing me a headache for a long time. It's the problem that the pictures taken by the 3-star mobile phone rotate. There is the function of uploading pictures in the project, so when it comes to taking pictures and selecting pictures from the photo album, other mobile phones have no problem with ok, but after taking pictures with a 3-star mobile phone, you will clearly see that the photos will be rotated for 1 time, and then the pictures you find according to the path have been rotated, and the solution has finally been found by me. We can read the rotation angle in the information of photo exif (Exchangeable Image File exchangeable image file) according to the path of the picture. As for this EXIF, we can see Daniel's article

EXIF under Android

According to debugging, it can be clearly found that the rotation angle of the picture taken by the 3-star mobile phone is 90 degrees, while the rotation angle of other mobile phones is 0 degrees

Look at the code under 1:


/** 
  *  Read a photo exif Rotation angle in information  
  * @param path  Photo path  
  * @return Angle  
  */ 
 public static int readPictureDegree(String path) { 
  int degree = 0; 
  try { 
   ExifInterface exifInterface = new ExifInterface(path); 
   int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
   switch (orientation) { 
    case ExifInterface.ORIENTATION_ROTATE_90: 
     degree = 90; 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_180: 
     degree = 180; 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_270: 
     degree = 270; 
     break; 
   } 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
  return degree; 
 } 

Then we just need to rotate the picture according to the rotation angle, and then OK


public static Bitmap toturn(Bitmap img){ 
  Matrix matrix = new Matrix(); 
  matrix.postRotate(+90); /* Flip 90 Degrees */ 
  int width = img.getWidth(); 
  int height =img.getHeight(); 
  img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true); 
  return img; 
 } 

It's solved easily, isn't it perfect?
The above is the whole content of this article, I hope you like it.


Related articles: