Shirish Herwade Shirish Herwade - 1 year ago 174
Android Question

why image captured using camera intent gets rotated on some devices in android

I'm capturing image and setting it to image view.

public void captureImage() {
Intent intentCamera = new Intent("");
File filePhoto = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
imageUri = Uri.fromFile(filePhoto);
MyApplicationGlobal.imageUri = imageUri.getPath();
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intentCamera, TAKE_PICTURE);

protected void onActivityResult(int requestCode, int resultCode, Intent intentFromCamera) {
super.onActivityResult(requestCode, resultCode, intentFromCamera);
if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
if (intentFromCamera != null) {
Bundle extras = intentFromCamera.getExtras();
if (extras.containsKey("data")) {
bitmap = (Bitmap) extras.get("data");
} else {
bitmap = getBitmapFromUri();
} else {
bitmap = getBitmapFromUri();
// imageView.setImageBitmap(bitmap);
} else {

public Bitmap getBitmapFromUri() {
getContentResolver().notifyChange(imageUri, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
return bitmap;
} catch (Exception e) {
return null;

But problem is, the image on some devices everytime it gets rotated. e.g. on Samsung device it works good, on Sony Experia image gets rotated by 90 degree and on Toshiba thrive(tab) by 180 degree

Answer Source

Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the EXIF data with the orientation that the photo should be viewed in.

Note that below solution depends on the camera software/device manufacturer populating the EXIF data, so it will work in most cases, but it is not a 100% reliable solution.

ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,

switch(orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotateImage(bitmap, 90);
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotateImage(bitmap, 180);
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotateImage(bitmap, 270);
    case ExifInterface.ORIENTATION_NORMAL:

Here is the rotateImage method:

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,