Skip to content

Commit ca71197

Browse files
committed
Fix image rotated and file path
1 parent 6ceac92 commit ca71197

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

compressor/src/main/java/id/zelory/compressor/Compressor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Compressor {
2424
private String destinationDirectoryPath;
2525

2626
public Compressor(Context context) {
27-
destinationDirectoryPath = context.getCacheDir().getPath() + File.pathSeparator + "Compressor";
27+
destinationDirectoryPath = context.getCacheDir().getPath() + File.separator + "images";
2828
}
2929

3030
public Compressor setMaxWidth(int maxWidth) {
@@ -61,7 +61,7 @@ public File compressToFile(File imageFile, String compressedFileName) throws IOE
6161
destinationDirectoryPath + File.separator + compressedFileName);
6262
}
6363

64-
public Bitmap compressToBitmap(File imageFile) {
64+
public Bitmap compressToBitmap(File imageFile) throws IOException {
6565
return ImageUtil.decodeSampledBitmapFromFile(imageFile, maxWidth, maxHeight);
6666
}
6767

@@ -86,7 +86,11 @@ public Flowable<Bitmap> compressToBitmapAsFlowable(final File imageFile) {
8686
return Flowable.defer(new Callable<Flowable<Bitmap>>() {
8787
@Override
8888
public Flowable<Bitmap> call() {
89-
return Flowable.just(compressToBitmap(imageFile));
89+
try {
90+
return Flowable.just(compressToBitmap(imageFile));
91+
} catch (IOException e) {
92+
return Flowable.error(e);
93+
}
9094
}
9195
});
9296
}

compressor/src/main/java/id/zelory/compressor/ImageUtil.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
5+
import android.graphics.Matrix;
6+
import android.media.ExifInterface;
57

68
import java.io.File;
79
import 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

Comments
 (0)