7

In my code below, I am trying to take photo using native camera and upload to server but when I take it as portrait and view it in gallery as landscape which means, its rotated to 90 degree. Pls help :-

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, REQUEST_CAMERA);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {

            handleCameraPhoto();
}
private void handleCameraPhoto() {
    Intent mediaScanIntent = new Intent(
            "android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);

    mediaScanIntent.setData(contentUri);
    getActivity().sendBroadcast(mediaScanIntent);
}

How can I rotate the image before saving to SD card?

Bette Devine
  • 1,196
  • 1
  • 9
  • 23
micky
  • 235
  • 2
  • 4
  • 16
  • 1
    Bear in mind that *some* Android devices take portrait photos and save them at portrait orientation, and *some* Android devices take portrait photos and save them at landscape orientation, adding rotation information to the EXIF metadata to indicate that they're actually portrait. (See http://stackoverflow.com/questions/3647993/android-bitmaps-loaded-from-gallery-are-rotated-in-imageview/3648017#3648017). You need to figure out which is happening and correct only if necessary. (You may need no correction on-device if you can fix your server to take notice of the EXIF rotation data.) – Matt Gibson Oct 22 '13 at 08:19
  • @MattGibson i'm still lost. So is it better i don't do anything? I'm new to android. I just refer the link and how to merge into my code? – micky Oct 22 '13 at 08:31
  • Possible duplicate of [why image captured using camera intent gets rotated on some devices in android](http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android) – Shirish Herwade Jun 27 '16 at 06:21
  • Answer present here http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android – Shirish Herwade Aug 23 '16 at 07:46

2 Answers2

24

I also faced this kind of problem while showing the images in listview. But using the EXIF data I was able to get a work around to set the images in proper orientation.

This is were the bitmap object for display is prepared :

  Matrix matrix = new Matrix();
  matrix.postRotate(getImageOrientation(url));
  Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
  bitmap.getHeight(), matrix, true);

This is the method used, in the 2nd line of above code, to rotate the images orientation.

 public static int getImageOrientation(String imagePath){
     int rotate = 0;
     try {

         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;
         }
     } catch (IOException e) {
         e.printStackTrace();
     }
    return rotate;
 }

This may not be the precise answer to your question, it worked for me and hope it will be useful for you.

Stefan
  • 5,203
  • 8
  • 27
  • 51
Shail Adi
  • 1,502
  • 4
  • 14
  • 35
  • i got 1 more question. If i wants to rotate the capture photo before saving into sd card where should i put this code into? Inside onActivityResult? – micky Oct 25 '13 at 04:27
  • Its more or less saving of the bytearray through stream, when you save any image to sdcard. That's why exifinterface class is used for saving orientation, location etc data. I have tried doing what you have mentioned, but was not able to. In my opinion try to maintain the above code while displaying imageviews (cause that's where you mostly use images). – Shail Adi Oct 29 '13 at 13:08
  • This worked for me! I tested using Samsung Galaxy Y Gingerbread and Samsung Galaxy S3 Ice Cream Sandwich. Will this work in all phone models running Android? Thanks a lot. :) – KarenAnne Dec 19 '13 at 09:37
  • what is the variable `bitmap` here? – Shajeel Afzal Apr 01 '15 at 17:51
  • what is the variable url here? as I need no involvement of url here. just need to copy gallery photo in sdcard and it gets rotated @ShailAdi – Narendra Singh May 12 '15 at 14:56
2
Matrix matrix=new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);



This method does not require creating a new bitmap each time.. Hope this works.
AKSH
  • 201
  • 1
  • 12
  • For more help follow this link...http://stackoverflow.com/questions/8981845/androidrotate-image-in-imageview-by-an-angle – AKSH Oct 22 '13 at 08:12
  • 3
    Please, give credits when you just copy & paste another one's answer! – Alexander Vogt Oct 22 '13 at 08:16
  • @Amit if i postRotate to 90 degree will it for all the phone same? Because i test on HTC it work fine no need to rotate only for samsung. Please correct me if i'm wrong – micky Oct 22 '13 at 08:25