Skip to content

Commit

Permalink
Added android equivalent code to iOS code. (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikunj authored and ivpusic committed Jul 3, 2018
1 parent 3059918 commit 97ce8ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.reactnative.ivpusic.imagepicker;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableMap;
import id.zelory.compressor.Compressor;
import java.util.Arrays;
import java.util.List;

import java.io.File;
import java.io.IOException;
Expand All @@ -17,12 +20,19 @@

class Compression {

File compressImage(final Activity activity, final ReadableMap options, final String originalImagePath) throws IOException {
File compressImage(final Activity activity, final ReadableMap options, final String originalImagePath, final BitmapFactory.Options bitmapOptions) throws IOException {
Integer maxWidth = options.hasKey("compressImageMaxWidth") ? options.getInt("compressImageMaxWidth") : null;
Integer maxHeight = options.hasKey("compressImageMaxHeight") ? options.getInt("compressImageMaxHeight") : null;
Double quality = options.hasKey("compressImageQuality") ? options.getDouble("compressImageQuality") : null;

if (maxWidth == null && maxHeight == null && quality == null) {
Boolean isLossLess = (quality == null || quality == 1.0);
Boolean useOriginalWidth = (maxWidth == null || maxWidth >= bitmapOptions.outWidth);
Boolean useOriginalHeight = (maxHeight == null || maxHeight >= bitmapOptions.outHeight);

List knownMimes = Arrays.asList("image/jpeg", "image/jpg", "image/png", "image/gif", "image/tiff");
Boolean isKnownMimeType = (bitmapOptions.mimeType != null && knownMimes.contains(bitmapOptions.mimeType.toLowerCase()));

if (isLossLess && useOriginalWidth && useOriginalHeight && isKnownMimeType) {
Log.d("image-crop-picker", "Skipping image compression");
return new File(originalImagePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@ private WritableMap getImage(final Activity activity, String path) throws Except
if (path.startsWith("http://") || path.startsWith("https://")) {
throw new Exception("Cannot select remote files");
}
validateImage(path);
BitmapFactory.Options original = validateImage(path);

// if compression options are provided image will be compressed. If none options is provided,
// then original image will be returned
File compressedImage = compression.compressImage(activity, options, path);
File compressedImage = compression.compressImage(activity, options, path, original);
String compressedImagePath = compressedImage.getPath();
BitmapFactory.Options options = validateImage(compressedImagePath);
long modificationDate = new File(path).lastModified();
Expand Down

0 comments on commit 97ce8ba

Please sign in to comment.