2014-01-13 125 views
4

對於圖像,我有MediaStore.Images.Media.DATA uri如何使用該uri獲取MediaStore.Images.ImageColumns.ORIENTATION?我得到一個NullPointerException。從MediaStore.Images.Media.DATA獲取圖像的方向

以下是我的代碼,

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()的代碼行。

+0

'cursor' is null? – gunar

+0

@ gunar是的,它是。 – ultimate

回答

6

使用此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; 
    } 
+0

這不適用於三星A3 2016. –

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

其實這兩個答案是正確的,他們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; 
    } 
} 
+0

非常漂亮和乾淨的代碼。謝謝@Fox的襪子。它不適用於5.1.1設備:( – Manisha

+0

@Manisha在哪個設備上有問題?剛剛在Samsung Note 4上使用5.1.1測試此代碼 – eleven

+0

我現在正在使用5.1.1測試Samsung Edge 6 – Manisha