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
Author: Wu Zhigang, deputy chief physician, First...
For your product, choosing the right keywords for...
Many people may not have thought that GAC Trumpch...
The specific strategies for enterprises to implem...
In the era of universal IP, many brands are tryin...
A few days ago, I received such a patient, 62-yea...
Unlike the quiet river valley, dense poplar fores...
01 What is WeChat Moments Advertising? WeChat Mom...
The page ranking factors related to keywords are ...
Nowadays, driven by the strategy of building a st...
Course Contents: 1. Analysis and Monetization Log...
Li Xingxing: Editing Practice Training Camp Resou...
When Alimama launched the red envelope promotion ...
66 million years ago, an asteroid about 10 kilome...
Before I start sharing how to build a traffic cir...