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
LeTV Sports is about to break into the e-sports c...
Apple introduced an exciting new feature called &...
[[263433]] With the approach of macOS 10.15 and i...
October 16th of every year is World Spine Day bec...
Even though Baidu is very rogue and has been refu...
Since the emergence of the novel coronavirus Omic...
Operate the local delivery ordering group project...
This is an age of information overload. Consumers...
The recently released OnePlus 8Pro has gained a g...
2022 NetEase video brick moving to make money, da...
Millet [March traffic event] Registration is open...
ChatGPT is very popular now. As an artificial int...
We know that Android can handle multiple tasks at...
As a marketing planner, planning promotion activi...
Written by | Wulianhuakai Many people have this e...