4

i have MediaStore.Images.Media.DATA uri for an image how I can get MediaStore.Images.ImageColumns.ORIENTATION using that uri ? I am getting a NullPointerException.

Following is my code,

private  int getOrientation(Context context, Uri photoUri) {

Log.v("orientatioon", "not crashed01");
Cursor cursor = context.getContentResolver().query(photoUri,
        new String[] { MediaStore.Images.ImageColumns._ID,MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
Log.v("orientatioon", "not crashed02");


cursor.moveToFirst();
Log.v("orientatioon", "not crashed 03");
int i=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION));
Log.v("orientatioon", ""+i);
cursor.close();
return i;
}

I am getting a NullPointerException at cursor.moveToFirst() line of code.

0

3 Answers 3

22

Actually both answers are right and they must be used simultaneously.

/**
 * @return 0, 90, 180 or 270. 0 could be returned if there is no data about rotation
 */
public static int getImageRotation(Context context, Uri imageUri) {
    try {
        ExifInterface exif = new ExifInterface(imageUri.getPath());
        int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

        if (rotation == ExifInterface.ORIENTATION_UNDEFINED)
            return getRotationFromMediaStore(context, imageUri);
        else return exifToDegrees(rotation);
    } catch (IOException e) {
        return 0;
    }
}

public static int getRotationFromMediaStore(Context context, Uri imageUri) {
    String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION};
    Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
    if (cursor == null) return 0;

    cursor.moveToFirst();

    int orientationColumnIndex = cursor.getColumnIndex(columns[1]);
    return cursor.getInt(orientationColumnIndex);
}

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;
    } else {
        return 0;
    }
}
7
  • Very nice and clean code. thanks @Fox in socks. It dose not work with 5.1.1 device :(
    – Manisha
    Dec 4, 2015 at 20:48
  • @Manisha On which device you have a problem? Tested this code just now on Samsung Note 4 with 5.1.1.
    – eleven
    Dec 4, 2015 at 21:04
  • 1
    Out of all the answers everywhere this is the one that actually works in 2016. Great stuff. Aug 8, 2016 at 4:22
  • 2
    do you need to close the cursor before returning?
    – Jason
    Oct 9, 2017 at 1:29
  • 1
    Interesting that you choose to read from the Exif file first, then fallback to MediaStore. Do you distrust media store? (It is much faster than using ExifInterface) Feb 25, 2021 at 10:52
5

Use this method to get the Orientation

public static int getExifOrientation(String filepath) {// YOUR MEDIA PATH AS STRING
        int degree = 0;
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (exif != null) {
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
                }

            }
        }
        return degree;
    }
0
5

Please do like this. have a try

 final Uri imageUri = data.getData();

                        String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION};

                        Cursor cursor = getContentResolver().query(imageUri, columns, null, null, null);


                        if (cursor == null) {

                            return;
                        }

                        cursor.moveToFirst();

                        int columnIndex = cursor.getColumnIndex(columns[0]);
                        int orientationColumnIndex = cursor.getColumnIndex(columns[1]);


                        String filePath = cursor.getString(columnIndex);
                        int orientation = cursor.getInt(orientationColumnIndex);

                        Log.d(TAG, "got image orientation "+orientation);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.