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

我有一个图像的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.

解决方法:

使用此方法获取方向

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;
    }
上一篇:当Orientation改变android时,Activity重启?


下一篇:android – 屏幕方向更改时,不得忽略对话框