user846566
user846566

Reputation: 395

How to know if picture is landscape or portrait?

I have pictures in my Gallery that are both landscape or portrait. The show up correctly in the Gallery application. When I use an intent to select the picture from the gallery I get a URI back. But before I display the picture how do I know if the picture is portrait or landscape?

My application selects pictures using an Intent like this:

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

Here is how I get the intent back:

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

Upvotes: 4

Views: 4125

Answers (3)

user846566
user846566

Reputation: 395

After the above "Answers" I wrote the following method. Hopefully this will help someone else.

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

Upvotes: 1

K_Anas
K_Anas

Reputation: 31466

Use this inside 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]));
                    }  

And use Matrix object to rotate your images

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

Upvotes: 6

t0mm13b
t0mm13b

Reputation: 34592

There's a MediaStore.Images.Media.ORIENTATION field used by the content provider.

The code would need to be slightly modified by including the field to tell you what orientation the image is at which is expressed in degrees, 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.

Upvotes: 3

Related Questions