22
33import android .graphics .Bitmap ;
44import android .graphics .BitmapFactory ;
5+ import android .graphics .Matrix ;
6+ import android .media .ExifInterface ;
57
68import java .io .File ;
79import java .io .FileOutputStream ;
@@ -21,6 +23,10 @@ private ImageUtil() {
2123
2224 static File compressImage (File imageFile , int reqWidth , int reqHeight , Bitmap .CompressFormat compressFormat , int quality , String destinationPath ) throws IOException {
2325 FileOutputStream fileOutputStream = null ;
26+ File file = new File (destinationPath ).getParentFile ();
27+ if (!file .exists ()) {
28+ file .mkdirs ();
29+ }
2430 try {
2531 fileOutputStream = new FileOutputStream (destinationPath );
2632 // write the compressed bitmap at the destination specified by destinationPath.
@@ -35,7 +41,7 @@ static File compressImage(File imageFile, int reqWidth, int reqHeight, Bitmap.Co
3541 return new File (destinationPath );
3642 }
3743
38- static Bitmap decodeSampledBitmapFromFile (File imageFile , int reqWidth , int reqHeight ) {
44+ static Bitmap decodeSampledBitmapFromFile (File imageFile , int reqWidth , int reqHeight ) throws IOException {
3945 // First decode with inJustDecodeBounds=true to check dimensions
4046 BitmapFactory .Options options = new BitmapFactory .Options ();
4147 options .inJustDecodeBounds = true ;
@@ -46,7 +52,23 @@ static Bitmap decodeSampledBitmapFromFile(File imageFile, int reqWidth, int reqH
4652
4753 // Decode bitmap with inSampleSize set
4854 options .inJustDecodeBounds = false ;
49- return BitmapFactory .decodeFile (imageFile .getAbsolutePath (), options );
55+
56+ Bitmap scaledBitmap = BitmapFactory .decodeFile (imageFile .getAbsolutePath (), options );
57+
58+ //check the rotation of the image and display it properly
59+ ExifInterface exif ;
60+ exif = new ExifInterface (imageFile .getAbsolutePath ());
61+ int orientation = exif .getAttributeInt (ExifInterface .TAG_ORIENTATION , 0 );
62+ Matrix matrix = new Matrix ();
63+ if (orientation == 6 ) {
64+ matrix .postRotate (90 );
65+ } else if (orientation == 3 ) {
66+ matrix .postRotate (180 );
67+ } else if (orientation == 8 ) {
68+ matrix .postRotate (270 );
69+ }
70+ scaledBitmap = Bitmap .createBitmap (scaledBitmap , 0 , 0 , scaledBitmap .getWidth (), scaledBitmap .getHeight (), matrix , true );
71+ return scaledBitmap ;
5072 }
5173
5274 private static int calculateInSampleSize (BitmapFactory .Options options , int reqWidth , int reqHeight ) {
0 commit comments