get Bitmap Orientation loaded from Uri - Android Graphics

Android examples for Graphics:Bitmap URL

Description

get Bitmap Orientation loaded from Uri

Demo Code


//package com.java2s;

import android.media.ExifInterface;
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class Main {
    public static int getOrientation(Uri photoUri) {
        File imageFile = new File(photoUri.getPath());

        try {//from w  ww. j  a v  a2  s.c  o  m
            ExifInterface exif = new ExifInterface(
                    imageFile.getAbsolutePath());
            // We only recognize a subset of orientation tag modes
            switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_UNDEFINED)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return 90;
            case ExifInterface.ORIENTATION_ROTATE_180:
                return 180;
            case ExifInterface.ORIENTATION_ROTATE_270:
                return 270;
            default:
                return ExifInterface.ORIENTATION_UNDEFINED;
            }
        } catch (IOException e) {
            Log.e("WallOfLightApp", e.getMessage());
            return 0;
        }
    }
}

Related Tutorials