Android Image Orientation Get getOrientation(Context context, Uri imageUri)

Here you can find the source of getOrientation(Context context, Uri imageUri)

Description

Gets the orientation.

License

Open Source License

Parameter

Parameter Description
context the context
imageUri the image uri

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Return

the orientation

Declaration

public static int getOrientation(Context context, Uri imageUri)
        throws IOException 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.IOException;
import android.content.Context;
import android.database.Cursor;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore.MediaColumns;

public class Main {
    /**//  ww  w .  j av a  2s .c o m
     * Gets the orientation.
     * 
     * @param context
     *            the context
     * @param imageUri
     *            the image uri
     * @return the orientation
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static int getOrientation(Context context, Uri imageUri)
            throws IOException {
        String[] filePathColumn = { MediaColumns.DATA };
        Cursor cursor = context.getContentResolver().query(imageUri,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        return getCameraPhotoOrientation(context, imageUri, filePath);
    }

    /**
     * Gets the camera photo orientation.
     * 
     * @param context
     *            the context
     * @param imageUri
     *            the image uri
     * @param imagePath
     *            the image path
     * @return the camera photo orientation
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static int getCameraPhotoOrientation(Context context,
            Uri imageUri, String imagePath) throws IOException {
        int rotate = 0;
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imagePath);

        ExifInterface exif = new android.media.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;
        }
        return rotate;
    }
}

Related

  1. getOriginalFilePath(Context context, Uri imageUri)