Skip to content Skip to sidebar Skip to footer

Get Orientation Of Image From Mediastore.images.media.data

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

Solution 1:

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
 */publicstaticintgetImageRotation(Context context, Uri imageUri) {
    try {
        ExifInterfaceexif=newExifInterface(imageUri.getPath());
        introtation= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

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

publicstaticintgetRotationFromMediaStore(Context context, Uri imageUri) {
    String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION};
    Cursorcursor= context.getContentResolver().query(imageUri, columns, null, null, null);
    if (cursor == null) return0;

    cursor.moveToFirst();

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

privatestaticintexifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
        return90;
    } elseif (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
        return180;
    } elseif (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
        return270;
    } else {
        return0;
    }
}

Solution 2:

Use this method to get the Orientation

publicstaticintgetExifOrientation(String filepath) {// YOUR MEDIA PATH AS STRINGintdegree=0;
        ExifInterfaceexif=null;
        try {
            exif = newExifInterface(filepath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (exif != null) {
            intorientation= 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;
    }

Solution 3:

Please do like this. have a try

finalUriimageUri= data.getData();

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

                        Cursorcursor= getContentResolver().query(imageUri, columns, null, null, null);


                        if (cursor == null) {

                            return;
                        }

                        cursor.moveToFirst();

                        intcolumnIndex= cursor.getColumnIndex(columns[0]);
                        intorientationColumnIndex= cursor.getColumnIndex(columns[1]);


                        StringfilePath= cursor.getString(columnIndex);
                        intorientation= cursor.getInt(orientationColumnIndex);

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

Post a Comment for "Get Orientation Of Image From Mediastore.images.media.data"