从MediaStore.Images.Media.DATA获取图像的方向

4

我有一张图片的MediaStore.Images.Media.DATA uri,如何使用该uri获取MediaStore.Images.ImageColumns.ORIENTATION? 我得到了一个NullPointerException。

以下是我的代码:

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;
}

我的代码中,在cursor.moveToFirst()这一行出现了NullPointerException

3个回答

22

实际上两个答案都是正确的,并且它们必须同时使用。

/**
 * @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;
    }
}

非常好的干净代码。感谢@Fox in socks。它不适用于5.1.1设备 :( - Manisha
@Manisha 你遇到了哪个设备的问题?我刚刚在安装有5.1.1系统的三星Note 4上测试了这段代码。 - eleven
1
在所有的答案中,这个才是真正适用于2016年的。非常好。 - Keith Adler
2
你需要在返回之前关闭游标吗? - Jason
1
有趣的是你选择先从Exif文件中读取,然后再回退到MediaStore。你不信任MediaStore吗?(使用ExifInterface比使用MediaStore快得多) - Ben Butterworth
显示剩余2条评论

5
请按照以下步骤操作,试一试:

请按照以下步骤操作,试一试

 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);

5
使用此方法获取方向
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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接