【问题标题】:get orientation of image from MediaStore.Images.Media.DATA从 MediaStore.Images.Media.DATA 获取图像的方向
【发布时间】:2014-01-13 06:25:39
【问题描述】:

我有 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

【问题讨论】:

    标签: android image nullpointerexception orientation uri


    【解决方案1】:

    其实两个答案都是对的,must可以同时使用。

    /**
     * @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 穿着袜子。它不适用于 5.1.1 设备:(
    • @Manisha 您在哪个设备上遇到问题?刚刚在带有 5.1.1 的三星 Note 4 上测试了这段代码。
    • 我目前正在使用 5.1.1 在 Samsung Edge 6 上进行测试
    • 在所有答案中,这是 2016 年真正有效的答案。很棒的东西。
    • 返回前是否需要关闭光标?
    【解决方案2】:

    使用此method 获取方向

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

    【讨论】:

      【解决方案3】:

      请这样做。试试看

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

      【讨论】:

        猜你喜欢
        • 2020-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多