Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
react-native-image-picker
Advanced tools
A React Native module that allows you to use native UI to select media from the device library or directly from the camera
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.
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);
}
});
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.
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.
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.
A React Native module that allows you to select a photo/video from the device library or camera.
yarn add react-native-image-picker
# RN >= 0.60
cd ios && pod install
# RN < 0.60
react-native link react-native-image-picker
Add the appropriate keys to your Info.plist,
If you are allowing user to select image/video from photos, add NSPhotoLibraryUsageDescription
.
If you are allowing user to capture image add NSCameraUsageDescription
key also.
If you are allowing user to capture video add NSCameraUsageDescription
add NSMicrophoneUsageDescription
key also.
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
.
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
launchCamera()
Launch camera to take photo or video.
launchCamera(options?, callback);
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)
See Options for further information on options
.
The callback
will be called with a response object, refer to The Response Object.
Option | iOS | Android | Description |
---|---|---|---|
mediaType | OK | OK | 'photo' or 'video' or 'mixed'(mixed supported only for launchImageLibrary, to pick an photo or video) |
maxWidth | OK | OK | To resize the image |
maxHeight | OK | OK | To resize the image |
videoQuality | OK | OK | 'low', 'medium', or 'high' on iOS, 'low' or 'high' on Android |
durationLimit | OK | OK | Video max duration in seconds |
quality | OK | OK | 0 to 1, photos |
cameraType | OK | OK | 'back' or 'front'. May not be supported in few android devices |
includeBase64 | OK | OK | If true, creates base64 string of the image (Avoid using on large image files due to performance) |
saveToPhotos | OK | OK | (Boolean) Only for launchCamera, saves the image/video file captured to public photo |
selectionLimit | OK | OK | Default is 1 , use 0 to allow any number of files. Only iOS version >= 14 support 0 and also it supports providing any integer value |
key | iOS | Android | Description |
---|---|---|---|
didCancel | OK | OK | true if the user cancelled the process |
errorCode | OK | OK | Check ErrorCode for all error codes |
errorMessage | OK | OK | Description of the error, use it for debug purpose only |
assets | OK | OK | Array of the selected media, refer to Asset Object |
key | iOS | Android | Description |
---|---|---|---|
base64 | OK | OK | The base64 string of the image (photos only) |
uri | OK | OK | 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 |
width | OK | OK | Image dimensions (photos only) |
height | OK | OK | Image dimensions (photos only) |
fileSize | OK | OK | The file size (photos only) |
type | OK | OK | The file type (photos only) |
fileName | OK | OK | The file name |
duration | OK | OK | The selected video duration in seconds |
Image/video captured via camera will be stored in temporary folder so will 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 (You have to obtain the permission, the library does not).
Code | Description |
---|---|
camera_unavailable | camera not available on device |
permission | Permission not satisfied |
others | other errors (check errorMessage for description) |
FAQs
A React Native module that allows you to use native UI to select media from the device library or directly from the camera
The npm package react-native-image-picker receives a total of 193,369 weekly downloads. As such, react-native-image-picker popularity was classified as popular.
We found that react-native-image-picker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.