Last active
May 11, 2016 05:38
-
-
Save ryanlntn/bc298bdea9f09696ea2aeb562a1a8d8e to your computer and use it in GitHub Desktop.
ImagePickerModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImagePickerModule extends ReactContextBaseJavaModule { | |
private static final int PICK_IMAGE = 1; | |
private Callback pickerSuccessCallback; | |
private Callback pickerCancelCallback; | |
@ReactMethod | |
public void openSelectDialog(ReadableMap config, Callback successCallback, Callback cancelCallback) { | |
Activity currentActivity = getCurrentActivity(); | |
if (currentActivity == null) { | |
cancelCallback.invoke("Activity doesn't exist"); | |
return; | |
} | |
pickerSuccessCallback = successCallback; | |
pickerCancelCallback = cancelCallback; | |
try { | |
final Intent galleryIntent = new Intent(); | |
galleryIntent.setType("image/*"); | |
galleryIntent.setAction(Intent.ACTION_GET_CONTENT); | |
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Pick an image"); | |
currentActivity.startActivityForResult(chooserIntent, PICK_IMAGE); | |
} catch (Exception e) { | |
cancelCallback.invoke(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment