Search code examples
androidandroid-intentandroid-cameraandroid-imageandroid-camera-intent

Android sending captured image rotates 90 degree in some devices


I am working on an Android application. In my app I have to capture a image and send that image to server. In some device the captured image posted in server with 90 degree rotation. I searched in stackoverflow and some other sites for a fix. I got solutions ..I used all them For eg:

Uri selectedImage = data.getData();


File imageFile = new File(selectedImage.toString());
ExifInterface exif;
try {
exif = new ExifInterface(imageFile.getAbsolutePath());

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:

rotate=90;

    break;
    case ExifInterface.ORIENTATION_ROTATE_180:

    rotate=180;
 break;
            }

But unfortunately I am getting orientation 0 always in every device. Even in the 90 degree rotated image devices.

Please help to fix my issue friends.


Solution

  • I fixed my issue by using the following code.

    private int getImageOrientation(){
        final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION };
        final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
        Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                imageColumns, null, null, imageOrderBy);
    
        if(cursor.moveToFirst()){
            int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
            rotate=orientation;
            System.out.println("orientation==="+orientation);
            cursor.close();
            return orientation;
        } else {
            return 0;
        }
    }
    

    Thanks for your response dear friends...