Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : How to detect the image orientation (portrait or landscape) picked from gallery while setting on an imageview?

I am setting an image on the imageview picked from the gallery(camera album). If the picked image has landscape orientation, it displays perfectly but if the image in in portrait mode(i.e the image was clicked in portrait mode) it is displaying the image with a 90 degree rotation. Now I am trying to find out the orientation just before setting on imageview, but all the images are giving same orientation and same width-height. Here is my code :

Uri selectedImage = intent.getData(); if (selectedImage != null) {     Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);      int str = new ExifInterface(selectedImage.getPath()).getAttributeInt("Orientation", 1000);     Toast.makeText(this, "value:" + str, Toast.LENGTH_LONG).show();     Toast.makeText(this, "width:" + bitmap.getWidth() + "height:" + bitmap.getHeight(), Toast.LENGTH_LONG).show(); 

portrait modelandscape mode

like image 721
sarabhai05 Avatar asked Oct 04 '12 12:10

sarabhai05


People also ask

How do I manage portrait and landscape on Android?

Rotate your phone to change the screen orientation (if Auto Rotate is enabled). If Auto Rotate is enabled, your phone's screen will automatically flip to portrait mode when you are holding it upright. When you are holding it horizontally, it will automatically switch to Landscape mode.

How do I view screen orientation on Android?

int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.


2 Answers

Use ExifInterface for rotate image. Use this method for get correct value to be rotate captured image from camera.

public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){     int rotate = 0;     try {         context.getContentResolver().notifyChange(imageUri, null);         File imageFile = new File(imagePath);          ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());         int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);          switch (orientation) {         case ExifInterface.ORIENTATION_ROTATE_270:             rotate = 270;             break;         case ExifInterface.ORIENTATION_ROTATE_180:             rotate = 180;             break;         case ExifInterface.ORIENTATION_ROTATE_90:             rotate = 90;             break;         }          Log.i("RotateImage", "Exif orientation: " + orientation);         Log.i("RotateImage", "Rotate value: " + rotate);     } catch (Exception e) {         e.printStackTrace();     }     return rotate; } 

And put this code in Activity result method and get value to rotate image...

String selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA};  Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst();  int columnIndex = cursor.getColumnIndex(filePathColumn[0]); filePath = cursor.getString(columnIndex); cursor.close();  int rotateImage = getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath); 

Hope this helps..

like image 101
Deepak Avatar answered Sep 22 '22 11:09

Deepak


This is also working for me:

String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; Cursor cur = getContentResolver().query(imageUri, orientationColumn, null, null, null); int orientation = -1; if (cur != null && cur.moveToFirst()) {     orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); } Matrix matrix = new Matrix(); matrix.postRotate(orientation); 
like image 31
sarabhai05 Avatar answered Sep 21 '22 11:09

sarabhai05