When developing Android apps with React Native, you may need to use modules that are not encapsulated by React Native. But you can write native modules in Java and then selectively expose public interfaces to React Native. Let's try it together! What are we going to write? At the time of writing this article, React Native includes an ImagePickerIOS component, but there is no corresponding ImagePicker component on the Android platform. We will now build a simple ImagePicker for Android that is roughly similar to ImagePickerIOS. Writing an Android native module for React Native requires the following steps:
Let’s practice this together. Create a ReactPackage Launch AndroidStudio and navigate to MyApp/android/app/src/main/java/com/myapp/MainActivity.java. It should look like this:
Let's first import a package that has not yet been defined:
Now let's write that package. We'll create a new directory for it called imagepicker and write ImagePickerPackage:
Now we have created a package and included it in MainActivity. Create a ReactContextBaseJavaModule We will start by creating ImagePickerModule, which extends ReactContextBaseJavaModule.
This is a good start, in order for React Native to find our module from NativeModules, we need to override the getName method.
Now that we have a native module that can be imported by JavaScript code, let's make it do something interesting. Exposure method ImagePickerIOS defines an openSelectDialog method that can be passed a configuration object and callbacks for failure and success. Let's define a similar method in ImagePickerModule.
Here we import Callback and ReadableMap from React Native to correspond to function and object in JavaScript. We annotate this method with @ReactMethod so that it can be referenced by JavaScript as part of ImagePicker. In the method body we get the current activity, and if there is no activity, we call the cancel callback method. We now have a functioning method, but it doesn't do anything interesting yet. Let's use it to open the photo album.
First, we set up the callback, then we create an Intent and pass it to startActivityForResult. Finally, we wrap everything in a try/catch block to handle any exceptions that might occur. When you call openSelectDialog, you should be able to see an album. However, when you select a picture, the album does not do anything. In order to be able to process the image data, we need to handle the return value of the activity in the module. First, we need to add the activity event listener to the react context:
Now we can get the data returned by the album.
Here we should be able to get the image URI through the success callback.
To roughly mimic the behavior of ImagePickerIOS, we can allow the user to select an image, video, or open the camera directly. The writing of these functions is basically the same as above, and we will leave it as an exercise for the reader. |
<<: Contract Programming vs Defensive Programming
>>: Android Development Universal Rounded Corner ImageView
Recently, a type of fruit-flavored e-cigarette ha...
Following the developer conferences of Microsoft ...
The refrigerator is a weird place. A half-eaten w...
When operating a community , you will inevitably ...
According to recent news, according to the sales ...
Part-time jobs and online earning are all opened ...
Douban's cold start was quite successful. Of ...
Two dramas are very popular recently. One is call...
To further reduce the tax burden on taxpayers and ...
This set of training courses includes Fanyin’s Ku...
Being able to organize a perfect hit event is def...
What I talk about most with friends recently is K...
On May 7, the Shanghai Municipal Epidemic Preventi...
WeChat is now 7 years old. When you have graduall...
[[138837]] Yesterday, Xunlei CEO Zou Shenglong pu...