2
2
3
3
import android .graphics .Bitmap ;
4
4
import android .graphics .BitmapFactory ;
5
+ import android .graphics .Matrix ;
6
+ import android .media .ExifInterface ;
5
7
6
8
import java .io .File ;
7
9
import java .io .FileOutputStream ;
@@ -21,6 +23,10 @@ private ImageUtil() {
21
23
22
24
static File compressImage (File imageFile , int reqWidth , int reqHeight , Bitmap .CompressFormat compressFormat , int quality , String destinationPath ) throws IOException {
23
25
FileOutputStream fileOutputStream = null ;
26
+ File file = new File (destinationPath ).getParentFile ();
27
+ if (!file .exists ()) {
28
+ file .mkdirs ();
29
+ }
24
30
try {
25
31
fileOutputStream = new FileOutputStream (destinationPath );
26
32
// 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
35
41
return new File (destinationPath );
36
42
}
37
43
38
- static Bitmap decodeSampledBitmapFromFile (File imageFile , int reqWidth , int reqHeight ) {
44
+ static Bitmap decodeSampledBitmapFromFile (File imageFile , int reqWidth , int reqHeight ) throws IOException {
39
45
// First decode with inJustDecodeBounds=true to check dimensions
40
46
BitmapFactory .Options options = new BitmapFactory .Options ();
41
47
options .inJustDecodeBounds = true ;
@@ -46,7 +52,23 @@ static Bitmap decodeSampledBitmapFromFile(File imageFile, int reqWidth, int reqH
46
52
47
53
// Decode bitmap with inSampleSize set
48
54
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 ;
50
72
}
51
73
52
74
private static int calculateInSampleSize (BitmapFactory .Options options , int reqWidth , int reqHeight ) {
0 commit comments