android - Image is getting rotated when clicked from camera only in Samasung device -


in application image getting rotated when clicked camera in portrait mode, happens samsung device , rest works fine. implemented following code after researching in stack overflow:

exifinterface ei = new exifinterface(imgfile.getpath()); int orientation = ei.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_undefined);  switch (orientation) { case exifinterface.orientation_undefined: mbitmap = rotateimage(bitmap, 90); break; } 

this code helps me fix issue in samsung when image clicked camera getting rotated in other devices due piece of code.

please let me know how can fix issue.

use below class

 string path="path of image";   imageview.setimagebitmap(exifutil.rotatebitmap(path, bitmapfactory.decodefile(path))); 

exifutil.java

public class exifutil {     /**      * @see http://sylvana.net/jpegcrop/exif_orientation.html      */     public static bitmap rotatebitmap(string src, bitmap bitmap) {         try {             int orientation = getexiforientation(src);              if (orientation == 1) {                 return bitmap;             }              matrix matrix = new matrix();             switch (orientation) {             case 2:                 matrix.setscale(-1, 1);                 break;             case 3:                 matrix.setrotate(180);                 break;             case 4:                 matrix.setrotate(180);                 matrix.postscale(-1, 1);                 break;             case 5:                 matrix.setrotate(90);                 matrix.postscale(-1, 1);                 break;             case 6:                 matrix.setrotate(90);                 break;             case 7:                 matrix.setrotate(-90);                 matrix.postscale(-1, 1);                 break;             case 8:                 matrix.setrotate(-90);                 break;             default:                 return bitmap;             }              try {                 bitmap oriented = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight(), matrix, true);                 bitmap.recycle();                 return oriented;             } catch (outofmemoryerror e) {                 e.printstacktrace();                 return bitmap;             }         } catch (ioexception e) {             e.printstacktrace();         }           return bitmap;     }      private static int getexiforientation(string src) throws ioexception {         int orientation = 1;          try {             /**              * if targeting api level >= 5              * exifinterface exif = new exifinterface(src);              * orientation = exif.getattributeint(exifinterface.tag_orientation, 1);              */             if (build.version.sdk_int >= 5) {                 class<?> exifclass = class.forname("android.media.exifinterface");                 constructor<?> exifconstructor = exifclass.getconstructor(new class[] { string.class });                 object exifinstance = exifconstructor.newinstance(new object[] { src });                 method getattributeint = exifclass.getmethod("getattributeint", new class[] { string.class, int.class });                 field tagorientationfield = exifclass.getfield("tag_orientation");                 string tagorientation = (string) tagorientationfield.get(null);                 orientation = (integer) getattributeint.invoke(exifinstance, new object[] { tagorientation, 1});             }         } catch (classnotfoundexception e) {             e.printstacktrace();         } catch (securityexception e) {             e.printstacktrace();         } catch (nosuchmethodexception e) {             e.printstacktrace();         } catch (illegalargumentexception e) {             e.printstacktrace();         } catch (instantiationexception e) {             e.printstacktrace();         } catch (illegalaccessexception e) {             e.printstacktrace();         } catch (invocationtargetexception e) {             e.printstacktrace();         } catch (nosuchfieldexception e) {             e.printstacktrace();         }          return orientation;     } } 

Comments