title: Using exif to rotate an image
body: <p>i was following this link <a href="https://stackoverflow.com/questions/7286714/android-get-orientation-of-a-camera-bitmap-and-rotate-back-90-degrees">Android get Orientation of a camera Bitmap? And rotate back -90 degrees</a> to rotate my image if/when necessary, but its not working for me, i get this error</p>
  
  <p>05-28 23:29:30.049 9735-9735/ss.sealstudios.com.socialstories E/JHEAD: can't open '/document/508'</p>
  
  <p>but checking it like below looks like there pointing in the right place</p>
  
  <p>05-28 23:29:30.049 9735-9735/ss.sealstudios.com.socialstories I/System.out: bitmap uri content://com.android.providers.downloads.documents/document/508 0</p>
  
  <p>//the 0 on the end is me checking for the rotation</p>
  
  <p>i was wondering if this was build version specific like getting the real path from a URI as running this code on my marshmallow device gives me an entirely different result (this result, ie: error codes, is from a kitkat device), but this has defeated me for some weeks now following all sorts of answers to no avail, can anyone weigh in and help me please, here's what I'm trying to do</p>
  
  <pre><code>   if (requestCode == PICK_IMAGE_REQUEST &amp;&amp; resultCode == RESULT_OK &amp;&amp; data 
     != null 
     &amp;&amp; data.getData() != null) 
    {
  
          Uri uri = data.getData();
  
  
          try {
              BitmapFactory.Options bitmapOptions = new 
              BitmapFactory.Options();
              bitmapOptions.inSampleSize = (int) 4;
              InputStream inputStream = 
              getContentResolver().openInputStream(uri);
              Bitmap scaledBitmap = BitmapFactory.decodeStream(inputStream, 
              null, bitmapOptions);
              ExifInterface exif = new ExifInterface(uri.getPath());
              int rotation = 
              exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,  
              ExifInterface.ORIENTATION_NORMAL);
              int rotationInDegrees = exifToDegrees(rotation);
              System.out.println("bitmap uri " + uri + " " + rotation);
              Matrix matrix = new Matrix();
  
              if (rotation != 0f){
                  matrix.preRotate(rotationInDegrees);
                  Bitmap.createBitmap(scaledBitmap, 0, 
                  0,scaledBitmap.getWidth(),scaledBitmap.getHeight(), 
                  matrix, true);
                  imageView.setImageBitmap(scaledBitmap);
  
              }else
                  imageView.setImageBitmap(scaledBitmap);
  
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
      }
  }
  private static int exifToDegrees(int exifOrientation) {
      if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { 
      return 90; 
      }
      else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {   
      return 180; }
      else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  
      return 270; }
      return 0;
  }
  </code></pre>
  
  <p>many thanks</p>