mirror of
https://github.com/status-im/react-native.git
synced 2025-01-26 17:30:25 +00:00
Update docs on using onActivityResult (again)
Summary: Update the long image picker example Closes https://github.com/facebook/react-native/pull/10234 Differential Revision: D3967223 fbshipit-source-id: c99a98c1d83de9804406362fcddb7db0b83b08fa
This commit is contained in:
parent
bbade77fb4
commit
9ac9ec90c3
@ -344,7 +344,7 @@ public void onActivityResult(
|
||||
We will implement a simple image picker to demonstrate this. The image picker will expose the method `pickImage` to JavaScript, which will return the path of the image when called.
|
||||
|
||||
```java
|
||||
public class ImagePickerModule extends ReactContextBaseJavaModule implements ActivityEventListener {
|
||||
public class ImagePickerModule extends ReactContextBaseJavaModule {
|
||||
|
||||
private static final int IMAGE_PICKER_REQUEST = 467081;
|
||||
private static final String E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST";
|
||||
@ -353,12 +353,36 @@ public class ImagePickerModule extends ReactContextBaseJavaModule implements Act
|
||||
private static final String E_NO_IMAGE_DATA_FOUND = "E_NO_IMAGE_DATA_FOUND";
|
||||
|
||||
private Promise mPickerPromise;
|
||||
|
||||
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
|
||||
|
||||
@Override
|
||||
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == IMAGE_PICKER_REQUEST) {
|
||||
if (mPickerPromise != null) {
|
||||
if (resultCode == Activity.RESULT_CANCELED) {
|
||||
mPickerPromise.reject(E_PICKER_CANCELLED, "Image picker was cancelled");
|
||||
} else if (resultCode == Activity.RESULT_OK) {
|
||||
Uri uri = intent.getData();
|
||||
|
||||
if (uri == null) {
|
||||
mPickerPromise.reject(E_NO_IMAGE_DATA_FOUND, "No image data found");
|
||||
} else {
|
||||
mPickerPromise.resolve(uri.toString());
|
||||
}
|
||||
}
|
||||
|
||||
mPickerPromise = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public ImagePickerModule(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
|
||||
// Add the listener for `onActivityResult`
|
||||
reactContext.addActivityEventListener(this);
|
||||
reactContext.addActivityEventListener(mActivityEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -391,28 +415,6 @@ public class ImagePickerModule extends ReactContextBaseJavaModule implements Act
|
||||
mPickerPromise = null;
|
||||
}
|
||||
}
|
||||
|
||||
// You can get the result here
|
||||
@Override
|
||||
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
|
||||
if (requestCode == IMAGE_PICKER_REQUEST) {
|
||||
if (mPickerPromise != null) {
|
||||
if (resultCode == Activity.RESULT_CANCELED) {
|
||||
mPickerPromise.reject(E_PICKER_CANCELLED, "Image picker was cancelled");
|
||||
} else if (resultCode == Activity.RESULT_OK) {
|
||||
Uri uri = intent.getData();
|
||||
|
||||
if (uri == null) {
|
||||
mPickerPromise.reject(E_NO_IMAGE_DATA_FOUND, "No image data found");
|
||||
} else {
|
||||
mPickerPromise.resolve(uri.toString());
|
||||
}
|
||||
}
|
||||
|
||||
mPickerPromise = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user