5.0.1 • Published 1 year ago

react-native-image-picker-test v5.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

React Native Image Picker

A React Native module that allows you to select a photo/video from the device library or camera.

Make sure you're reading the doc applicable to your version, for example if you're using version 3.8.0 go to tag 3.8.0 and read those docs. This doc is always that of main branch.

Also read version release notes for any breaking changes especially if you're updating the major version.

Install

yarn add react-native-image-picker

# RN >= 0.60
cd ios && pod install

# RN < 0.60
react-native link react-native-image-picker

Post-install Steps

iOS

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.

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);

// You can also use as a promise without '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)

// You can also use as a promise without '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

OptioniOSAndroidWebDescription
mediaTypeOKOKOK'photo' or 'video' or 'mixed'(mixed supported only for launchImageLibrary, to pick an photo or video). Web only suppots 'photo' for now.
maxWidthOKOKNOTo resize the image
maxHeightOKOKNOTo resize the image
videoQualityOKOKNO'low', 'medium', or 'high' on iOS, 'low' or 'high' on Android
durationLimitOKOKNOVideo max duration in seconds
qualityOKOKNO0 to 1, photos
cameraTypeOKOKNO'back' or 'front'. May not be supported in few android devices
includeBase64OKOKOKIf true, creates base64 string of the image (Avoid using on large image files due to performance)
includeExtraOKOKNOIf true, will include extra data which requires library permissions to be requested (i.e. exif data)
saveToPhotosOKOKNO(Boolean) Only for launchCamera, saves the image/video file captured to public photo
selectionLimitOKOKOKDefault is 1, use 0 to allow any number of files. Only iOS version >= 14 & Android version >= 13 support 0 and also it supports providing any integer value
presentationStyleOKNONOControls how the picker is presented. 'pageSheet', 'fullScreen', 'pageSheet', 'formSheet', 'popover', 'overFullScreen', 'overCurrentContext'. Default is 'currentContext'

The Response Object

keyiOSAndroidWebDescription
didCancelOKOKOKtrue if the user cancelled the process
errorCodeOKOKOKCheck ErrorCode for all error codes
errorMessageOKOKOKDescription of the error, use it for debug purpose only
assetsOKOKOKArray of the selected media, refer to Asset Object

Asset Object

keyiOSAndroidWebPhoto/VideoRequires PermissionsDescription
base64OKOKOKPHOTO ONLYNOThe base64 string of the image (photos only)
uriOKOKOKBOTHNOThe 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.
widthOKOKOKBOTHNOAsset dimensions
heightOKOKOKBOTHNOAsset dimensions
fileSizeOKOKNOBOTHNOThe file size
typeOKOKNOBOTHNOThe file type
fileNameOKOKNOBOTHNOThe file name
durationOKOKNOVIDEO ONLYNOThe selected video duration in seconds
bitrate---OKNOVIDEO ONLYNOThe average bitrate (in bits/sec) of the selected video, if available. (Android only)
timestampOKOKNOBOTHYESTimestamp of the asset. Only included if 'includeExtra' is true
idOKOKNOBOTHYESlocal identifier of the photo or video. On Android, this is the same as fileName
originFilepathNOOKNOPHOTO ONLYYESthr origin file path when select image

waring: the uri is point at a temporary file! When you pick a same image twice, it will be two different uri, the fileName also to be the temporary file's name.

Note on file storage

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).

For web this doesn't work.

ErrorCode

CodeDescription
camera_unavailablecamera not available on device
permissionPermission not satisfied
othersother errors (check errorMessage for description)

License

MIT