What is react-native-image-picker?
The react-native-image-picker package allows React Native developers to access the device's camera and photo library. It provides a simple way to capture images and videos or select them from the device's gallery.
What are react-native-image-picker's main functionalities?
Launch Camera
This feature allows you to launch the device's camera to capture a photo. The `launchCamera` function takes an options object to specify the media type and camera type, and a callback function to handle the response.
import { launchCamera } from 'react-native-image-picker';
const options = {
mediaType: 'photo',
cameraType: 'back',
};
launchCamera(options, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
const source = { uri: response.uri };
console.log('Image URI: ', source.uri);
}
});
Launch Image Library
This feature allows you to open the device's image library to select a photo. The `launchImageLibrary` function takes an options object to specify the media type and a callback function to handle the response.
import { launchImageLibrary } from 'react-native-image-picker';
const options = {
mediaType: 'photo',
};
launchImageLibrary(options, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
const source = { uri: response.uri };
console.log('Image URI: ', source.uri);
}
});
Video Recording
This feature allows you to launch the device's camera to record a video. The `launchCamera` function takes an options object to specify the media type as video and the camera type, and a callback function to handle the response.
import { launchCamera } from 'react-native-image-picker';
const options = {
mediaType: 'video',
cameraType: 'back',
};
launchCamera(options, (response) => {
if (response.didCancel) {
console.log('User cancelled video recording');
} else if (response.error) {
console.log('VideoPicker Error: ', response.error);
} else {
const source = { uri: response.uri };
console.log('Video URI: ', source.uri);
}
});
Other packages similar to react-native-image-picker
react-native-camera
The react-native-camera package provides a comprehensive camera module for React Native. It supports photo and video capture, barcode scanning, and text recognition. Compared to react-native-image-picker, it offers more advanced camera functionalities but requires more setup.
expo-image-picker
The expo-image-picker package is part of the Expo ecosystem and provides similar functionalities to react-native-image-picker, such as selecting images and videos from the device's library or capturing them using the camera. It is easier to set up within an Expo project but may not offer as much flexibility as react-native-image-picker in a bare React Native project.
react-native-image-crop-picker
The react-native-image-crop-picker package offers image and video picking with cropping functionality. It provides a more customizable user interface for cropping images compared to react-native-image-picker, making it a good choice if cropping is a required feature.
react-native-image-picker 🎆
A React Native module that allows you to select a photo/video from the device library or camera.
Installation
yarn add react-native-image-picker
New Architecture
To take advantage of the new architecture run-
iOS
RCT_NEW_ARCH_ENABLED=1 npx pod-install ios
Android
Set newArchEnabled
to true
inside android/gradle.properties
Pre-Fabric (AKA not using the new architecture)
npx pod-install ios
Post-install Steps
iOS
Add the appropriate keys to your Info.plist
depending on your requirement:
Requirement | Key |
---|
Select image/video from photos | NSPhotoLibraryUsageDescription |
Capture Image | NSCameraUsageDescription |
Capture Video | NSCameraUsageDescription & NSCameraUsageDescription |
Android
No permissions required (saveToPhotos
requires permission check).
Note: This library does not require Manifest.permission.CAMERA
, if your app declares as using this permission in manifest then you have to obtain the permission before using launchCamera
.
API Reference
Methods
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
launchCamera()
Launch camera to take photo or video.
launchCamera(options?, callback);
const result = await launchCamera(options?);
See Options for further information on options
.
The callback
will be called with a response object, refer to The Response Object.
launchImageLibrary
Launch gallery to pick image or video.
launchImageLibrary(options?, callback)
const result = await launchImageLibrary(options?);
See Options for further information on options
.
The callback
will be called with a response object, refer to The Response Object.
Options
Option | iOS | Android | Web | Description |
---|
mediaType | OK | OK | OK | photo or video or mixed (launchCamera on Android does not support 'mixed'). Web only supports 'photo' for now. |
maxWidth | OK | OK | NO | To resize the image. |
maxHeight | OK | OK | NO | To resize the image. |
videoQuality | OK | OK | NO | low , medium , or high on iOS, low or high on Android. |
durationLimit | OK | OK | NO | Video max duration (in seconds). |
quality | OK | OK | NO | 0 to 1, photos. |
cameraType | OK | OK | NO | 'back' or 'front' (May not be supported in few android devices). |
includeBase64 | OK | OK | OK | If true , creates base64 string of the image (Avoid using on large image files due to performance). |
includeExtra | OK | OK | NO | If true , will include extra data which requires library permissions to be requested (i.e. exif data). |
saveToPhotos | OK | OK | NO | (Boolean) Only for launchCamera , saves the image/video file captured to public photo. |
selectionLimit | OK | OK | OK | Supports providing any integer value. Use 0 to allow any number of files on iOS version >= 14 & Android version >= 13. Default is 1 . |
presentationStyle | OK | NO | NO | Controls how the picker is presented. currentContext , pageSheet , fullScreen , formSheet , popover , overFullScreen , overCurrentContext . Default is currentContext . |
formatAsMp4 | OK | NO | NO | Converts the selected video to MP4 (iOS Only). |
assetRepresentationMode | OK | NO | NO | A mode that determines which representation to use if an asset contains more than one. Possible values: 'auto', 'current', 'compatible'. Default is 'auto'. |
The Response Object
key | iOS | Android | Web | Description |
---|
didCancel | OK | OK | OK | true if the user cancelled the process |
errorCode | OK | OK | OK | Check ErrorCode for all error codes |
errorMessage | OK | OK | OK | Description of the error, use it for debug purpose only |
assets | OK | OK | OK | Array of the selected media, refer to Asset Object |
Asset Object
key | iOS | Android | Web | Photo/Video | Requires Permissions | Description |
---|
base64 | OK | OK | OK | PHOTO ONLY | NO | The base64 string of the image (photos only) |
uri | OK | OK | OK | BOTH | NO | The file uri in app specific cache storage. Except when picking video from Android gallery where you will get read only content uri, to get file uri in this case copy the file to app specific storage using any react-native library. For web it uses the base64 as uri. |
width | OK | OK | OK | BOTH | NO | Asset dimensions |
height | OK | OK | OK | BOTH | NO | Asset dimensions |
fileSize | OK | OK | NO | BOTH | NO | The file size |
type | OK | OK | NO | BOTH | NO | The file type |
fileName | OK | OK | NO | BOTH | NO | The file name |
duration | OK | OK | NO | VIDEO ONLY | NO | The selected video duration in seconds |
bitrate | --- | OK | NO | VIDEO ONLY | NO | The average bitrate (in bits/sec) of the selected video, if available. (Android only) |
timestamp | OK | OK | NO | BOTH | YES | Timestamp of the asset. Only included if 'includeExtra' is true |
id | OK | OK | NO | BOTH | YES | local identifier of the photo or video. On Android, this is the same as fileName |
Note on file storage
Image/video captured via camera will be stored in temporary folder allowing it to be deleted any time, so don't expect it to persist. Use saveToPhotos: true
(default is false
) to save the file in the public photos. saveToPhotos
requires WRITE_EXTERNAL_STORAGE
permission on Android 28 and below (The permission has to obtained by the App manually as the library does not handle that).
For web, this doesn't work.
ErrorCode
Code | Description |
---|
camera_unavailable | Camera not available on device |
permission | Permission not satisfied |
others | Other errors (check errorMessage for description) |
License
MIT