Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Read from Exif if available, otherwise read from MediaStore</p> <pre><code>public static int getImageOrientation(Context context, String imagePath) { int orientation = getOrientationFromExif(imagePath); if(orientation &lt;= 0) { orientation = getOrientationFromMediaStore(context, imagePath); } return orientation; } private static int getOrientationFromExif(String imagePath) { int orientation = -1; try { ExifInterface exif = new ExifInterface(imagePath); 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; } } catch (IOException e) { Log.e(LOG_TAG, "Unable to get image exif orientation", e); } return orientation; } private static int getOrientationFromMediaStore(Context context, String imagePath) { Uri imageUri = getImageContentUri(context, imagePath); if(imageUri == null) { return -1; } String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION}; Cursor cursor = context.getContentResolver().query(imageUri, projection, null, null, null); int orientation = -1; if (cursor != null &amp;&amp; cursor.moveToFirst()) { orientation = cursor.getInt(0); cursor.close(); } return orientation; } private static Uri getImageContentUri(Context context, String imagePath) { String[] projection = new String[] {MediaStore.Images.Media._ID}; String selection = MediaStore.Images.Media.DATA + "=? "; String[] selectionArgs = new String[] {imagePath}; Cursor cursor = context.getContentResolver().query(IMAGE_PROVIDER_URI, projection, selection, selectionArgs, null); if (cursor != null &amp;&amp; cursor.moveToFirst()) { int imageId = cursor.getInt(0); cursor.close(); return Uri.withAppendedPath(IMAGE_PROVIDER_URI, Integer.toString(imageId)); } if (new File(imagePath).exists()) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATA, imagePath); return context.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } return null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload