SoFunction
Updated on 2025-04-08

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

Recently, I solved a problem that has caused me a long time headache, which is the problem of rotating pictures of Samsung's mobile phones. The project has the function of uploading pictures. If you are taking pictures, selecting pictures from the album. No problem with other phones. Only after taking pictures on Samsung's mobile phones, you will clearly see that the photos will rotate them, and the picture you find according to the path has been rotated. The solution was finally found by me. We can read the rotation angle in the photo exif (Exchangeable Image File) information based on the image path. As for this EXIF, you can read the article of Daniu

EXIF under Android

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

Take a look at the code:

/**
   * Read the rotation angle in the exif information of the photo
   * @param path Photo path
   * @return angle
   */ 
 public static int readPictureDegree(String path) { 
  int degree = 0; 
  try { 
   ExifInterface exifInterface = new ExifInterface(path); 
   int orientation = (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) { 
   (); 
  } 
  return degree; 
 } 

Then we just need to rotate the picture according to the rotation angle and it's OK

public static Bitmap toturn(Bitmap img){ 
  Matrix matrix = new Matrix(); 
  (+90); /*Flip 90 degrees*/ 
  int width = (); 
  int height =(); 
  img = (img, 0, 0, width, height, matrix, true); 
  return img; 
 } 

It was easily solved, isn't it perfect?
The above is all about this article, I hope you like it.