title: Android camera/picture orientation issues with Samsung Galaxy S3, S4, S5
body: <p>I am developing a camera application for Android API 16 to 21 which main and only purpose is to take <strong>portrait</strong> photo. I am able to take picture with several devices (Nexus 4, Nexus 5, HTC...) and have them correctly oriented (meaning that my preview equals the taken picture both in size and orientation).</p>
  
  <p>However I have tested my application on several other devices and some of them are giving me alot of trouble: Samsung Galaxy S3/S4/S5.</p>
  
  <p>On these three devices, the preview is correctly displayed, however the pictures returned by the method <code>onPictureTaken(final byte[] jpeg, Camera camera)</code> are always sideways.</p>
  
  <p>This is the Bitmap created from <code>byte[] jpeg</code> and displayed in the ImageView to my user just before saving it to the disk:</p>
  
  <p><img src="https://i.stack.imgur.com/cG0yXm.png" alt="preview"></p>
  
  <p>And here is the image once saved on the disk:</p>
  
  <p><img src="https://i.stack.imgur.com/EIsIUm.jpg" alt="disk"></p>
  
  <p>As you can see the image is completly stretched in the preview and wrongly rotated once saved on the disk.</p>
  
  <p>Here is my CameraPreview class (I obfuscated other methods since they had nothing to do with camera parameters):</p>
  
  <pre><code>public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
  {
      private SurfaceHolder surfaceHolder;
      private Camera camera;
  
      // Removed unnecessary code
  
      public void surfaceCreated(SurfaceHolder holder)
      {
          camera.setPreviewDisplay(holder);
          setCameraParameters();
          camera.startPreview();
      }
  
      private void setCameraParameters()
      {
          Camera.Parameters parameters = camera.getParameters();
          Camera.CameraInfo info = new Camera.CameraInfo();
          Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
  
          DisplayMetrics metrics = new DisplayMetrics();
          WindowManager windowManager = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
          windowManager.getDefaultDisplay().getMetrics(metrics);
          int rotation = windowManager.getDefaultDisplay().getRotation();
          int degrees = 0;
          switch (rotation)
          {
              case Surface.ROTATION_0:
                  degrees = 0;
                  break;
              case Surface.ROTATION_90:
                  degrees = 90;
                  break;
              case Surface.ROTATION_180:
                  degrees = 180;
                  break;
              case Surface.ROTATION_270:
                  degrees = 270;
                  break;
          }
          int rotate = (info.orientation - degrees + 360) % 360;
          parameters.setRotation(rotate);
  
          // Save Parameters
          camera.setDisplayOrientation(90);
          camera.setParameters(parameters);
      }
  }
  </code></pre>
  
  <p>How come this exact piece of code works for other devices except Samsung's one ?</p>
  
  <p>I tried to find answers on the following SO posts but nothing could help me so far:
  <a href="https://stackoverflow.com/questions/15808719/controlling-the-camera-to-take-pictures-in-portrait-doesnt-rotate-the-final-ima">this one</a> and <a href="https://stackoverflow.com/questions/19176038/camera-setdisplayorientation-function-is-not-working-for-samsung-galaxy-ace-wi">this other one</a>.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Implementing Joey Chong's answer does not changes anything:</p>
  
  <pre><code>public void onPictureTaken(final byte[] data, Camera camera)
  {
      try
      {
          File pictureFile = new File(...);
          Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
          FileOutputStream fos = new FileOutputStream(pictureFile);
          realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  
          int orientation = -1;
          ExifInterface exif = new ExifInterface(pictureFile.toString());
          int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,  ExifInterface.ORIENTATION_NORMAL);
  
          switch (exifOrientation)
          {
              case ExifInterface.ORIENTATION_ROTATE_270:
                  orientation = 270;
                  break;
              case ExifInterface.ORIENTATION_ROTATE_180:
                  orientation = 180;
                  break;
              case ExifInterface.ORIENTATION_ROTATE_90:
                  orientation = 90;
                  break;
              case ExifInterface.ORIENTATION_NORMAL:
                  orientation = 0;
                  break;
              default:
                  break;
          }
  
          fos.close();
  }
  </code></pre>
  
  <p>Here are the EXIF results I get for a working device:</p>
  
  <ul>
  <li>Orientation: 0</li>
  </ul>
  
  <p>And here the results for the S4:</p>
  
  <ul>
  <li>Orientation: 0</li>
  </ul>