Skip to content

Commit bf2f8ae

Browse files
author
Mohammed Rampurawala
committed
- Compressor.java : Added method for compressing and copying the image using the Uri.
- ImageUti.java : Method to fetch and compress the image using the Uri.
1 parent d698334 commit bf2f8ae

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import android.content.Context;
44
import android.graphics.Bitmap;
5+
import android.net.Uri;
56

67
import java.io.File;
78
import java.io.IOException;
9+
import java.lang.ref.WeakReference;
810
import java.util.concurrent.Callable;
911

1012
import io.reactivex.Flowable;
13+
import io.reactivex.annotations.Nullable;
1114

1215
/**
1316
* Created on : June 18, 2016
@@ -22,9 +25,11 @@ public class Compressor {
2225
private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
2326
private int quality = 80;
2427
private String destinationDirectoryPath;
28+
private WeakReference<Context> contextWeakReference;
2529

2630
public Compressor(Context context) {
2731
destinationDirectoryPath = context.getCacheDir().getPath() + File.separator + "images";
32+
contextWeakReference = new WeakReference<Context>(context);
2833
}
2934

3035
public Compressor setMaxWidth(int maxWidth) {
@@ -61,6 +66,14 @@ public File compressToFile(File imageFile, String compressedFileName) throws IOE
6166
destinationDirectoryPath + File.separator + compressedFileName);
6267
}
6368

69+
public @Nullable File compressToUri(Uri imageUri, String compressedFileName) throws IOException {
70+
if (contextWeakReference == null || contextWeakReference.get() == null) {
71+
return null;
72+
}
73+
return ImageUtil.compressImage(contextWeakReference.get(),imageUri, maxWidth, maxHeight, compressFormat, quality,
74+
destinationDirectoryPath + File.separator + compressedFileName);
75+
}
76+
6477
public Bitmap compressToBitmap(File imageFile) throws IOException {
6578
return ImageUtil.decodeSampledBitmapFromFile(imageFile, maxWidth, maxHeight);
6679
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package id.zelory.compressor;
22

3+
import android.content.Context;
34
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
56
import android.graphics.Matrix;
67
import android.media.ExifInterface;
8+
import android.net.Uri;
79

810
import java.io.File;
11+
import java.io.FileNotFoundException;
912
import java.io.FileOutputStream;
1013
import java.io.IOException;
14+
import java.io.InputStream;
1115

1216
/**
1317
* Created on : June 18, 2016
@@ -41,6 +45,46 @@ static File compressImage(File imageFile, int reqWidth, int reqHeight, Bitmap.Co
4145
return new File(destinationPath);
4246
}
4347

48+
static File compressImage(Context context, Uri imageUri, int reqWidth, int reqHeight, Bitmap.CompressFormat compressFormat, int quality, String destinationPath) throws IOException {
49+
FileOutputStream fileOutputStream = null;
50+
File file = new File(destinationPath).getParentFile();
51+
if (!file.exists()) {
52+
file.mkdirs();
53+
}
54+
try {
55+
fileOutputStream = new FileOutputStream(destinationPath);
56+
// write the compressed bitmap at the destination specified by destinationPath.
57+
decodeSampledBitmapFromUri(context, imageUri, reqWidth, reqHeight).compress(compressFormat, quality, fileOutputStream);
58+
} finally {
59+
if (fileOutputStream != null) {
60+
fileOutputStream.flush();
61+
fileOutputStream.close();
62+
}
63+
}
64+
65+
return new File(destinationPath);
66+
}
67+
68+
static Bitmap decodeSampledBitmapFromUri(Context context, Uri imageUri, int reqWidth, int reqHeight) throws FileNotFoundException {
69+
BitmapFactory.Options options = new BitmapFactory.Options();
70+
options.inJustDecodeBounds = true;
71+
InputStream inputStream = context.getContentResolver().openInputStream(imageUri);
72+
BitmapFactory.decodeStream(inputStream, null, options);
73+
74+
// Calculate inSampleSize
75+
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
76+
77+
// Decode bitmap with inSampleSize set
78+
options.inJustDecodeBounds = false;
79+
80+
Bitmap scaledBitmap = BitmapFactory.decodeStream(inputStream, null, options);
81+
82+
//check the rotation of the image and display it properly
83+
Matrix matrix = new Matrix();
84+
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
85+
return scaledBitmap;
86+
}
87+
4488
static Bitmap decodeSampledBitmapFromFile(File imageFile, int reqWidth, int reqHeight) throws IOException {
4589
// First decode with inJustDecodeBounds=true to check dimensions
4690
BitmapFactory.Options options = new BitmapFactory.Options();

0 commit comments

Comments
 (0)