title: EXIF orientation tag value always 0 for image taken with portrait camera app android
body: <p>I have a camera app in portrait mode which takes pictures from both front and back end cameras.I am saving the image in my sd card and try to find the corresponding exif value which gives 0 always.But i am getting the the expected exif orientation value for the other images stored in the device(like downloaded pictures).</p>
  
  <p>How can i fix this ? Can anyone help me out ?</p>
  
  <p>Here is the code used to save the picture and the find the orientation</p>
  
  <pre><code>PictureCallback myPictureCallback_JPG = new PictureCallback() {
  
      @Override
      public void onPictureTaken(byte[] arg0, Camera arg1) {
          // TODO Auto-generated method stub
  
  
                  try {
                      File APP_FILE_PATH = new File(Environment.getExternalStorageDirectory()
                              .getPath() + "/Myapp/");
                      if (!APP_FILE_PATH.exists()) {
                          APP_FILE_PATH.mkdirs();
                      }
                      File file = new File(APP_FILE_PATH, "image.jpg");
  
                      FileOutputStream fos = new FileOutputStream(file);
                      fos.write(arg0);
                      fos.close();
  imageFileUri=Uri.fromfile(file);                           getApplicationContext().getContentResolver().notifyChange(
                            imageFileUri, null);
                  sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                              Uri.parse("file://"
                                      + Environment.getExternalStorageDirectory())));
                      ExifInterface exif = new ExifInterface(file.getAbsolutePath());
                      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  
  
  
                  } catch (Exception e) {
  
                  }
  
  
  
      }
  };
  </code></pre>
  
  <p>Following is the code for surphace created and changed functions</p>
  
  <pre><code>@Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
  int height) {
  List&lt;Size&gt; sizes = parameters.getSupportedPreviewSizes();
  Size optimalSize = getOptimalPreviewSize(sizes, width, height);
  parameters.setPreviewSize(optimalSize.width, optimalSize.height);
  
  camera.setParameters(parameters);
  camera.startPreview();
   startPreview();
  
  }
   @Override
   public void surfaceCreated(SurfaceHolder holder) {
  
  
   try {
  
       if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.GINGERBREAD) {
            Camera.CameraInfo info=new Camera.CameraInfo();
  
            for (int i=0; i &lt; Camera.getNumberOfCameras(); i++) {
              Camera.getCameraInfo(i, info);
  
              if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                camera=Camera.open(i);
                defaultCameraId = i;
              }
            }
          }
  
          if (camera == null) {
            camera=Camera.open();
          }
  
  
  
  
          try {
              camera.setPreviewDisplay(surfaceHolder);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          Camera.Parameters parameters = camera.getParameters();
          android.hardware.Camera.CameraInfo info =
                  new android.hardware.Camera.CameraInfo();
          android.hardware.Camera.getCameraInfo(defaultCameraId, info);
          int rotation = this.getWindowManager().getDefaultDisplay()
                  .getRotation();
          if (Integer.parseInt(Build.VERSION.SDK) &gt;= 8)
          {
  
               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 result;
               if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                   result = (info.orientation + degrees) % 360;
                   result = (360 - result) % 360;  
               } else {  // back-facing
                   result = (info.orientation - degrees + 360) % 360;
               }
               camera.setDisplayOrientation(result);
  
          }
          else
          {
              parameters.set("orientation", "portrait");
          }
  
  
          camera.setParameters(parameters);
  
  
  
  } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } 
  
  
  }
   private Size getOptimalPreviewSize(List&lt;Size&gt; sizes, int w, int h) {
              final double ASPECT_TOLERANCE = 0.1;
              double targetRatio = (double) w / h;
              if (sizes == null) return null;
  
              Size optimalSize = null;
              double minDiff = Double.MAX_VALUE;
  
              int targetHeight = h;
  
  
              for (Size size : sizes) {
                  double ratio = (double) size.width / size.height;
                  if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE) continue;
                  if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                      optimalSize = size;
                      minDiff = Math.abs(size.height - targetHeight);
                  }
              }
  
  
              if (optimalSize == null) {
                  minDiff = Double.MAX_VALUE;
                  for (Size size : sizes) {
                      if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                          optimalSize = size;
                          minDiff = Math.abs(size.height - targetHeight);
                      }
                  }
              }
              return optimalSize;
          }
  </code></pre>