Almost all the apps today need to pick images from the device’s Gallery and Camera. React Native Image Picker is a module based on an Image Picker component that provides access to the system UI. It has become widely popular since it allows apps to select images from the Gallery or the Camera effectively and straightforwardly—especially in projects built through Cross-Platform App Development Services, where consistency across devices is key.
Below is a step-by-step guide to integrating this module into a React Native app.
First, create a New React Native Project. Once this is done, you’re ready to go.
Step 1: Install react-native-image-picker
First of all, you will need to install react-native-image-picker dependency in the React Native app. Add the package below:
# for npm npm install react-native-image-picker --save
# for yarn yarn add react-native-image-picker
# if RN >= 0.60 cd ios && pod install
# if RN < 0.60 react-native link react-native-image-picker
Take into account that in case RN < 0.60, once you have done react-native link react-native-image-picker, you will also have to add the package written above:
cd ios && pod install
Step 2: Set the permissions
After installation, you must set permissions to use react-native-image-picker for image features.
Permissions in an Android environment
To assign permissions for Android devices, go to Project > android > app > src > debug > AndroidManifest.xml and then add the following permissions:
Open /ios/rnImagePickerExample/Info.plist and add the permissions below:
<plist version="1.0">
<dict>
// ...
// add the following
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) would like access to your photo gallery</string>
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) would like to use your camera</string>
<key>NSPhotoLibraryAddUsageDescription</key>
Step 3: Use React Native Image Picker
The ImagePicker component offers many options. The following options are pretty easy to configure:
Title
Cancel Button Title
Take Photo Button Title
Choose From Library Button Title
Choose Which Library Title
Custom Buttons
Tint Color
Camera Type
Media Type
Max Width
Max Height
Quality
Video Quality
Duration Limit
Rotation
Allows Editing
No Data
Storage Options
Permission Denied
First, import the ImagePicker component: import ImagePicker from ‘react-native-image-picker’; And then replace the current code of App.js with the following:
Initiate the ImagePicker by clicking on the “Select File” button. Then you will see a screen with the following options: Take Photo, Choose From Library, and Choose a file from Custom Option. See it below:
When the user opens the app, they will see a screen like the one below asking for permissions. You must allow permissions to access photos, media, and files on the device.
Step 4: Directly Launch Camera in the React Native app
Access the Launch Camera method through the ImagePicker component. To this end, use the code below, similar to other approaches for accessing camera features in React Native, where direct hardware interaction is required:
}); And that’s it. Following the steps detailed above, you should be able to configure and implement React Native Image Picker in a React Native app. Now your app will be able to access the device’s library and take photos!
Want to learn more?
If this article was helpful, you may also find other React Native blogs interesting.