Skip to content

Commit

Permalink
RN 0.33 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
ivpusic committed Sep 11, 2016
1 parent 457c0ef commit 67241b9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity
private static final String E_ERROR_WHILE_CLEANING_FILES = "E_ERROR_WHILE_CLEANING_FILES";

private Promise mPickerPromise;
private Activity activity;

private boolean cropping = false;
private boolean multiple = false;
Expand Down Expand Up @@ -160,7 +159,7 @@ public void openCamera(final ReadableMap options, final Promise promise) {
return;
}

activity = getCurrentActivity();
Activity activity = getCurrentActivity();

if (activity == null) {
promise.reject(E_ACTIVITY_DOES_NOT_EXIST, "Activity doesn't exist");
Expand Down Expand Up @@ -197,7 +196,7 @@ public void openCamera(final ReadableMap options, final Promise promise) {

@ReactMethod
public void openPicker(final ReadableMap options, final Promise promise) {
activity = getCurrentActivity();
Activity activity = getCurrentActivity();

if (activity == null) {
promise.reject(E_ACTIVITY_DOES_NOT_EXIST, "Activity doesn't exist");
Expand Down Expand Up @@ -264,7 +263,7 @@ public static String getMimeType(String url) {
return type;
}

public WritableMap getSelection(Uri uri) throws Exception {
public WritableMap getSelection(Activity activity, Uri uri) throws Exception {
String path = RealPathUtil.getRealPathFromURI(activity, uri);
if (path == null || path.isEmpty()) {
throw new Exception("Cannot resolve image path.");
Expand All @@ -275,7 +274,7 @@ public WritableMap getSelection(Uri uri) throws Exception {
return getVideo(path, mime);
}

return getImage(uri, true);
return getImage(activity, uri, true);
}

public WritableMap getVideo(String path, String mime) {
Expand All @@ -297,7 +296,7 @@ public WritableMap getVideo(String path, String mime) {
return image;
}

private WritableMap getImage(Uri uri, boolean resolvePath) throws Exception {
private WritableMap getImage(Activity activity, Uri uri, boolean resolvePath) throws Exception {
WritableMap image = new WritableNativeMap();
String path = uri.getPath();

Expand All @@ -309,10 +308,19 @@ private WritableMap getImage(Uri uri, boolean resolvePath) throws Exception {
throw new Exception("Cannot resolve image path.");
}

if (path.startsWith("http://") || path.startsWith("https://")) {
throw new Exception("Cannot select remote files");
}

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeFile(path, options);

if (options.outMimeType == null || options.outWidth == 0 || options.outHeight == 0) {
throw new Exception("Invalid image selected");
}

image.putString("path", "file://" + path);
image.putInt("width", options.outWidth);
image.putInt("height", options.outHeight);
Expand All @@ -326,7 +334,7 @@ private WritableMap getImage(Uri uri, boolean resolvePath) throws Exception {
return image;
}

public void startCropping(Uri uri) {
public void startCropping(Activity activity, Uri uri) {
UCrop.Options options = new UCrop.Options();
options.setCompressionFormat(Bitmap.CompressFormat.JPEG);

Expand All @@ -337,7 +345,7 @@ public void startCropping(Uri uri) {
.start(activity);
}

public void imagePickerResult(final int requestCode, final int resultCode, final Intent data) {
public void imagePickerResult(Activity activity, final int requestCode, final int resultCode, final Intent data) {
if (mPickerPromise == null) {
return;
}
Expand All @@ -352,10 +360,10 @@ public void imagePickerResult(final int requestCode, final int resultCode, final
try {
// only one image selected
if (clipData == null) {
result.pushMap(getSelection(data.getData()));
result.pushMap(getSelection(activity, data.getData()));
} else {
for (int i = 0; i < clipData.getItemCount(); i++) {
result.pushMap(getSelection(clipData.getItemAt(i).getUri()));
result.pushMap(getSelection(activity, clipData.getItemAt(i).getUri()));
}
}

Expand All @@ -372,10 +380,10 @@ public void imagePickerResult(final int requestCode, final int resultCode, final
}

if (cropping) {
startCropping(uri);
startCropping(activity, uri);
} else {
try {
mPickerPromise.resolve(getSelection(uri));
mPickerPromise.resolve(getSelection(activity, uri));
} catch (Exception ex) {
mPickerPromise.reject(E_NO_IMAGE_DATA_FOUND, ex.getMessage());
}
Expand All @@ -384,7 +392,7 @@ public void imagePickerResult(final int requestCode, final int resultCode, final
}
}

public void cameraPickerResult(final int requestCode, final int resultCode, final Intent data) {
public void cameraPickerResult(Activity activity, final int requestCode, final int resultCode, final Intent data) {
if (mPickerPromise == null) {
return;
}
Expand All @@ -402,23 +410,23 @@ public void cameraPickerResult(final int requestCode, final int resultCode, fina
if (cropping) {
UCrop.Options options = new UCrop.Options();
options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
startCropping(uri);
startCropping(activity, uri);
} else {
try {
mPickerPromise.resolve(getImage(uri, true));
mPickerPromise.resolve(getImage(activity, uri, true));
} catch (Exception ex) {
mPickerPromise.reject(E_NO_IMAGE_DATA_FOUND, ex.getMessage());
}
}
}
}

public void croppingResult(final int requestCode, final int resultCode, final Intent data) {
public void croppingResult(Activity activity, final int requestCode, final int resultCode, final Intent data) {
if (data != null) {
final Uri resultUri = UCrop.getOutput(data);
if (resultUri != null) {
try {
mPickerPromise.resolve(getImage(resultUri, false));
mPickerPromise.resolve(getImage(activity, resultUri, false));
} catch (Exception ex) {
mPickerPromise.reject(E_NO_IMAGE_DATA_FOUND, ex.getMessage());
}
Expand All @@ -431,13 +439,13 @@ public void croppingResult(final int requestCode, final int resultCode, final In
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
public void onActivityResult(Activity activity, final int requestCode, final int resultCode, final Intent data) {
if (requestCode == IMAGE_PICKER_REQUEST) {
imagePickerResult(requestCode, resultCode, data);
imagePickerResult(activity, requestCode, resultCode, data);
} else if (requestCode == CAMERA_PICKER_REQUEST) {
cameraPickerResult(requestCode, resultCode, data);
cameraPickerResult(activity, requestCode, resultCode, data);
} else if (requestCode == UCrop.REQUEST_CROP) {
croppingResult(requestCode, resultCode, data);
croppingResult(activity, requestCode, resultCode, data);
}
}

Expand Down
7 changes: 4 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"dependencies": {
"react": "15.2.1",
"react-native": "0.31.0",
"react-native-image-crop-picker": "../"
"react": "^15.3.1",
"react-native": "^0.33.0",
"react-native-image-crop-picker": "../",
"react-native-video": "^0.9.0"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-image-crop-picker",
"version": "0.8.3",
"version": "0.9.0",
"description": "Select single or multiple images, with croping option",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -29,6 +29,6 @@
},
"homepage": "https://github.com/ivpusic/react-native-image-crop-picker#readme",
"peerDependencies": {
"react-native": ">=0.30.0"
"react-native": ">=0.33.0"
}
}

0 comments on commit 67241b9

Please sign in to comment.