androidandroid-imageandroid-bitmapandroid-image-capture

All vertical oriented images are strangely autorotated only on some devices


I have an app that take pictures from camera or gallery and shows the result in an imageview.

I only get the image with content provider and use this scale function

public Bitmap scaleim(Bitmap bitmap) {
       ...
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, resizedWidth, resizedHeight, false);
        return scaledBitmap;
    }

In my devices with android 5 everything works ok, now I have tested the same App on a my friend device with Android 7, and every pictures that is vertical oriented is automatically rotated to horizontal orientation. This looks really weird and I have no idea of what causes the issue.


Solution

  • Problem is not in scaling but captured images work differently as per the hardware.Before start scaling that should be rotated as per suitable devices. Here it is following code:

      Matrix matrix = new Matrix();
      matrix.postRotate(getImageOrientation(url));
      Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
      bitmap.getHeight(), matrix, true)
    
    public static int getImageOrientation(String imagePath){
         int rotate = 0;
         try {
    
             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;
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
        return rotate;
     }