2012-07-14 81 views
3

我的畫廊中有橫向或縱向照片。在Gallery應用程序中正確顯示。當我使用意圖從圖庫中選擇圖片時,我得到一個URI。但在顯示圖片之前,我怎麼知道圖片是縱向還是橫向?如何知道圖片是橫向還是縱向?

我的應用程序使用的意圖是這樣選擇的圖片:

private OnClickListener btnChooseFromLibraryListener = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.setType("image/*"); 
     startActivityForResult(intent, REQ_CODE_PICK_IMAGE); 
    } 
}; 

這裏是我得到的意圖回:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE: 
     if(resultCode == RESULT_OK){ 
      Uri selectedImage = imageReturnedIntent.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = cursor.getString(columnIndex); 
      cursor.close(); 

      SetPicture(filePath); 
     } 
    } 
} 

private void SetPicture(String filePath) { 
    Bitmap bm = BitmapFactory.decodeFile(filePath); 
    Log.d("TW", "Picture Path:" + filePath); 
    String size = String.format("Width:%d Height:%d", bm.getWidth(), bm.getHeight()); 
    Log.d("TW", size); 
    ivPicture.setImageBitmap(bm); 
    ui.setLastPicture(filePath);   
} 
+0

[可能的重複](http://stackoverflow.com/q/4517634/420015) – adneal 2012-07-14 16:01:05

回答

3

有由內容提供商使用的MediaStore.Images.Media.ORIENTATION場。

的代碼需要通過包括外地來告訴你orientation圖像就是在這度,0,90,180表示略作修改,270

String[] filePathColumn = { 
     MediaStore.Images.Media.DATA, 
     MediaStore.Images.Media.ORIENTATION 
}; 
Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, 
        null, 
        null, 
        null); 
cursor.moveToFirst(); 
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
int orientationIndex = cursor.getColumnIndex(filePathPathColumn[1]); 
String filePath = cursor.getString(columnIndex); 
String degreesOrientation = cursor.getString(orientationIndex); 
cursor.close(); 
// Now degreesOrientation will tell you exactly the rotation, as in 
int nDegrees = Integer.parse(degreesOrientation); 
// Check nDegrees - for example: if (nDegrees == 0 || nDegrees == 180) portrait. 
5

使用這裏面onActivityResult()

Uri selectedImage = imageReturnedIntent.getData(); 
        String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; 
        Cursor cur = managedQuery(selectedImage, orientationColumn, null, null, null); 
        int orientation = -1; 
        if (cur != null && cur.moveToFirst()) { 
         orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); 
        } 

並使用Matrix對象旋轉您的圖片

Matrix matrix = new Matrix(); 
    matrix.postRotate(orientation); 
1

上述「答案」後我寫了下面的方法。希望這會幫助別人。

private int GetRotateAngle(Uri imageUri) { 
    String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION}; 
    Cursor cursor = getContentResolver().query(imageUri, columns, null, null, null); 
    if (cursor == null) { return 0; } 
    cursor.moveToFirst(); 

    int orientationColumnIndex = cursor.getColumnIndex(columns[1]); 
    int orientation = cursor.getInt(orientationColumnIndex); 
    cursor.close(); 
    return orientation; 
} 
相關問題