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: Du Lian Audit│Ding Yi Editor: Zhao Jingyu...
Basic Tarot Master Crash Course Resource Introduc...
According to CNMO Mobile China, the WeChat Open C...
[[135660]] In March, Microsoft reached a cooperat...
Addendum 1: Although lying down with your eyes cl...
KFC’s social death event is coming! In the past t...
OPPO R9 is destined to be another controversial p...
April 27 Ningbo CDC issues reminder What is the j...
gossip I believe you have seen this statement on ...
Nowadays, not only works of art, but as long as v...
The play is based on one of the controversial topi...
Following the release and hot sales of iPhone 6 a...
Half is flame, and the other half may also be fla...
Let me share a new term, the HOOKED addiction mod...
At the early morning of November 21, Beijing time...