77import android .graphics .Bitmap ;
88import android .graphics .BitmapFactory ;
99import android .util .Log ;
10+ import androidx .annotation .Nullable ;
1011import java .io .ByteArrayOutputStream ;
1112import java .io .File ;
1213import java .io .FileOutputStream ;
@@ -28,31 +29,39 @@ class ImageResizer {
2829 * <p>If no resizing is needed, returns the path for the original image.
2930 */
3031 String resizeImageIfNeeded (
31- String imagePath , Double maxWidth , Double maxHeight , int imageQuality ) {
32+ String imagePath ,
33+ @ Nullable Double maxWidth ,
34+ @ Nullable Double maxHeight ,
35+ @ Nullable Integer imageQuality ) {
3236 boolean shouldScale =
33- maxWidth != null || maxHeight != null || (imageQuality > -1 && imageQuality < 101 );
34-
35- if (!shouldScale ) {
36- return imagePath ;
37+ maxWidth != null || maxHeight != null || isImageQualityValid (imageQuality );
38+ String [] pathParts = imagePath .split ("/" );
39+ String imageName = pathParts [pathParts .length - 1 ];
40+ File file ;
41+ Bitmap bmp = decodeFile (imagePath );
42+ if (bmp == null ) {
43+ return null ;
3744 }
38-
3945 try {
40- File scaledImage = resizedImage (imagePath , maxWidth , maxHeight , imageQuality );
41- exifDataCopier .copyExif (imagePath , scaledImage .getPath ());
42-
43- return scaledImage .getPath ();
46+ if (!shouldScale ) {
47+ file = createImageOnExternalDirectory (imageName , bmp , 100 );
48+ } else {
49+ file = resizedImage (bmp , maxWidth , maxHeight , imageQuality , imageName );
50+ }
51+ copyExif (imagePath , file .getPath ());
52+ return file .getPath ();
4453 } catch (IOException e ) {
4554 throw new RuntimeException (e );
4655 }
4756 }
4857
49- private File resizedImage (String path , Double maxWidth , Double maxHeight , int imageQuality )
58+ private File resizedImage (
59+ Bitmap bmp , Double maxWidth , Double maxHeight , Integer imageQuality , String outputImageName )
5060 throws IOException {
51- Bitmap bmp = BitmapFactory .decodeFile (path );
5261 double originalWidth = bmp .getWidth () * 1.0 ;
5362 double originalHeight = bmp .getHeight () * 1.0 ;
5463
55- if (imageQuality < 0 || imageQuality > 100 ) {
64+ if (! isImageQualityValid ( imageQuality ) ) {
5665 imageQuality = 100 ;
5766 }
5867
@@ -91,24 +100,51 @@ private File resizedImage(String path, Double maxWidth, Double maxHeight, int im
91100 }
92101 }
93102
94- Bitmap scaledBmp = Bitmap .createScaledBitmap (bmp , width .intValue (), height .intValue (), false );
103+ Bitmap scaledBmp = createScaledBitmap (bmp , width .intValue (), height .intValue (), false );
104+ File file =
105+ createImageOnExternalDirectory ("/scaled_" + outputImageName , scaledBmp , imageQuality );
106+ return file ;
107+ }
108+
109+ private File createFile (File externalFilesDirectory , String child ) {
110+ return new File (externalFilesDirectory , child );
111+ }
112+
113+ private FileOutputStream createOutputStream (File imageFile ) throws IOException {
114+ return new FileOutputStream (imageFile );
115+ }
116+
117+ private void copyExif (String filePathOri , String filePathDest ) {
118+ exifDataCopier .copyExif (filePathOri , filePathDest );
119+ }
120+
121+ private Bitmap decodeFile (String path ) {
122+ return BitmapFactory .decodeFile (path );
123+ }
124+
125+ private Bitmap createScaledBitmap (Bitmap bmp , int width , int height , boolean filter ) {
126+ return Bitmap .createScaledBitmap (bmp , width , height , filter );
127+ }
128+
129+ private boolean isImageQualityValid (Integer imageQuality ) {
130+ return imageQuality != null && imageQuality > 0 && imageQuality < 100 ;
131+ }
132+
133+ private File createImageOnExternalDirectory (String name , Bitmap bitmap , int imageQuality )
134+ throws IOException {
95135 ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
96- boolean saveAsPNG = bmp .hasAlpha ();
136+ boolean saveAsPNG = bitmap .hasAlpha ();
97137 if (saveAsPNG ) {
98138 Log .d (
99139 "ImageResizer" ,
100140 "image_picker: compressing is not supported for type PNG. Returning the image with original quality" );
101141 }
102- scaledBmp .compress (
142+ bitmap .compress (
103143 saveAsPNG ? Bitmap .CompressFormat .PNG : Bitmap .CompressFormat .JPEG ,
104144 imageQuality ,
105145 outputStream );
106-
107- String [] pathParts = path .split ("/" );
108- String imageName = pathParts [pathParts .length - 1 ];
109-
110- File imageFile = new File (externalFilesDirectory , "/scaled_" + imageName );
111- FileOutputStream fileOutput = new FileOutputStream (imageFile );
146+ File imageFile = createFile (externalFilesDirectory , name );
147+ FileOutputStream fileOutput = createOutputStream (imageFile );
112148 fileOutput .write (outputStream .toByteArray ());
113149 fileOutput .close ();
114150 return imageFile ;
0 commit comments