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
When it comes to protein, many people's first...
Traffic costs are becoming more and more expensiv...
In the previous article, I wrote about using tool...
As long as you watch Tik Tok, you will find video...
introduce Implement a scroll view with parallax s...
Regarding Sina Weibo , let me first show you thre...
The latest data from iResearch MUT in April shows...
Audit expert: Yi Jingkun Zhihu famous geography a...
Produced by: Science Popularization China Author:...
Online activities must be able to produce short-t...
Durian, controversial because of its rich aroma, ...
[[405423]] Previously, many users were upset that...
Artificial intelligence continues to transform ev...
After the spring equinox, the temperature gradual...
In Android development, android:exported is an at...