如何知道图片是风景还是肖像?

use*_*566 4 android android-intent

我的画廊中有照片,无论是风景还是肖像.在Gallery应用程序中正确显示.当我使用意图从库中选择图片时,我得到了一个URI.但在我显示图片之前,如何知道图片是纵向还是横向?

我的应用程序使用Intent选择图片,如下所示:

    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);
    }
};
Run Code Online (Sandbox Code Playgroud)

以下是我如何获得意图:

    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);        
}
Run Code Online (Sandbox Code Playgroud)

K_A*_*nas 6

在里面使用它 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]));
                    }  
Run Code Online (Sandbox Code Playgroud)

并使用Matrix对象旋转图像

  Matrix matrix = new Matrix();
  matrix.postRotate(orientation);
Run Code Online (Sandbox Code Playgroud)