How to pick images from Camera & Gallery in React Native app (2024)

How to pick images from Camera & Gallery in React Nativeapp

How to pick images from Camera & Gallery in React Nativeapp (1)

If you would like access to the entire source code, you can check out the GitHub repo here. For step-by-step tutorial, continue reading below.

Picking images from Gallery and Camerais one of the most important and basic functionalities that is required in almost all the apps. Advanced functionalities are built upon this basic core facility. For picking the image in React Native, we will use a popular and superior library called react-native-image-picker. This provides the ImagePicker component in which you can provide the image picking option from Gallery or Camera.

We’ll follow a stepped approach to create an ImagePicker app in React Native. Following are the steps

  • Step 1 — Create a basic React Native app
  • Step 2 — Set up React Native Image Picker
  • Step 3 — Use React Native Image Picker to pick images in app

So let’s dive right in!

How to pick images from Camera & Gallery in React Nativeapp (2)

Step 1 —  Create a basic React Nativeapp

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App or we can run npx react-native init.

Assuming that you have node installed, you can use npm to install the react-native-cli command-line utility. Open the terminal and run the following command to the bottom

npm install -g react-native-cli

Now, let’s create a new React Native project, e.g. RNimagePicker

react-native init RNimagePicker

When the above command is done, open the main folder in your preferred editor. When we open the project in a code editor, its structure looks like this.

How to pick images from Camera & Gallery in React Nativeapp (3)

Step 2 — Set up React Native ImagePicker

To use React Native Image Picker we need to install react-native-image-picker dependency.

Installation of Dependency

To add React Native Image Pickerto our React Native Project, we need to run the following command at the root of the project

npm install react-native-image-picker --save
or
yarn add react-native-image-picker

This command will copy all the dependencies into your node_modules directory, You can find the directory in node_modules the directory named react-native-image-picker

Methods provided by react-native-image-picker

launchCamera() method launch the camera and helps us to take the photo. It returns the file data with different attributes like URI, base64 URL and other fields. More on this later.

launchImageLibrary() methods launch the Photo library and we can select photos and videos. It returns the file data with attributes like URI, base64 URL. More on this later.

Permissions

Android :-No permission needed for android.

iOS :-Adding appropriate keys to the 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.

Step — 3 How to Use React Native ImagePicker

React Native Image Picker library provides methods in which you can set the options like the title of the picker, your custom buttons (name and title of the button) and storage options like skipBackup, etc.

How to pick images from Camera & Gallery in React Nativeapp (4)

Launch Camera

To implement the functionality of launching the Camera as you click on “Direct Launch Camera”, and use the code below. It will open the camera and will show the clicked image on the Image Component.

How to pick images from Camera & Gallery in React Nativeapp (5)

Launch ImageLibrary

To Launch the Image Library you can use the following code below. It will directly open the Image Library and will show the Selected image on the Image Component.

How to pick images from Camera & Gallery in React Nativeapp (6)

App.js

Below is the explanation over methods used in appImagePicker.js file: -

  1. launchNativeImageLibrary [Line 83 in above code]:- This function is responsible to set the options (for image picker) and call the image picker method to open native library to select the image. This function is called on “Directly Launch Image Library” [Line 130].
  2. launchNativeCamera [Line 58] :-This function is responsible for setting camera options and calling image picker method to open camera. This function is called on “Directly Launch Camera” [Line 126].
  3. renderFileData [Line 30]:- This function is responsible for creating a base64 URL, that can be used to show the images in Image tag (image view).
  4. renderFileUri [Line 43]:- This function is responsible for using the file URI, that can be used to show the images in Image tag (image view).

This React Native module allows you to use native UI to select a photo/video from the device library as well as from the camera directly.

Your Screen should look like this:

How to pick images from Camera & Gallery in React Nativeapp (7)

iOS Simulator screens

Finally, for your reference all the screens for this image picker functionality will look similar to those shown below:

How to pick images from Camera & Gallery in React Nativeapp (8)

For iOS simulator, opening Camera won’t support but it will work on Real device.

React-native-image-picker vs Expo ImagePicker

As a final note, if you wondering about if this approach work in Expo also, the answer is — not exactly. you will need to use expo Imagepicker for that expo-image-picker

Conclusion

In this post, you learned in a quick fashion to implement React Native Image Picker in your React Native App. This enables your app to pick images/videos from the Camera and Gallery. It is one of the most important and basic functionalities that is needed in almost all the apps.
Still, Image Picker not working in React Native app, why? For this, you can find the complete code in this Github repo over here.

Next Steps

If you liked this blog, you will also find the following React Native blogs interesting and helpful. Feel free to ask any questions in the comment section

  • Firebase — Integrate Firebase | Analytics | Push notifications | Firebase CRUD
  • How To in React Native — Geolocation | Life cycle hooks | Image Picker | Redux implementation | Make API calls | Navigation | Translation | | | Google Vision | Pull to Refresh
  • Payments — Apple Pay | Stripe payments
  • Authentication — Google Login| Facebook login | Phone Auth | Twitter login
  • Create Instagram / Whatsapp Story Feature in React Native
  • React Native life cycle hooks | Implement Redux | Async actions with Redux
  • Create Awesome Apps in React Native using Full App

If you need a base to start your next React Native app, you can make your next awesome app using React Native Full App

How to pick images from Camera & Gallery in React Nativeapp (9)

How to pick images from Camera & Gallery in React Native app (2024)

FAQs

How to pick image from gallery and camera in React Native? ›

Here are the steps to implement an image picker in React Native:
  1. Step 1: Install react-native-image-picker. Open the terminal and navigate to your project directory. ...
  2. Step 2: Configure permissions. ...
  3. Step 4: Implement gallery image picker. ...
  4. Step 5: Implement camera image picker. ...
  5. Step 6: Trigger the image picker.

How to select an image in React Native? ›

React Native Image Picker: Allowing your App to Pick Images from the Gallery and Camera
  1. Step 1: Install react-native-image-picker. ...
  2. Step 2: Set the permissions. ...
  3. Step 3: Use React Native Image Picker. ...
  4. Step 4: Directly Launch Camera in the React Native app. ...
  5. Step 5: Directly Launch Image Library in the React Native app.
Aug 2, 2021

How do I select an image from the gallery in react native Expo? ›

For picking the image in React Native, we will use a popular and superior library called react-native-image-picker . This provides the ImagePicker component in which you can provide the image picking option from Gallery or Camera.

How do I upload an image from my camera to react native? ›

To handle image uploads we need to set the encoding type to multipart/form-data which means we need to format our data differently. Thus the createFormData function. This function will go ahead and take the image we selected and add it to the photo field of the form data with the required info.

How do I make an image gallery in react native? ›

In this tutorial, we're going to create a custom gallery of images using react-native-snap-carousel and FlatList from React Native, and the open-source library is going to display each image in a carousel view. The FlatList is what we will use to display the thumbnail view for each image below the carousel.

How do I use native camera in React Native? ›

Getting Started
  1. Installing the library​ Install react-native-vision-camera through npm: ...
  2. Updating manifests​ To use the Camera or Microphone you must first specify that your app requires camera and microphone permissions. ...
  3. Getting/Requesting Permissions​ ...
  4. Use the <Camera> view​

What is the use of image picker in React Native? ›

React-Native-Image-Picker is a React Native module that allows you to use native UI to select media from the device library or directly from the camera. It is currently used by almost 60 000 developers, as it is quite robust in what it offers.

What is an image picker? ›

Image Picker is a design-time dialog that allows you to select images from predefined sources, such as the DevExpress Icon Library, or create and manage custom icon collections for use in controls and image list components.

How do I get an image from local in react? ›

To reference a local image in React, you can follow these steps:
  1. Step 1: Import the Image File. ...
  2. Step 2: Use the Image in JSX. ...
  3. Step 3: Handling Different Image Formats. ...
  4. Step 4: Organizing Image Files. ...
  5. Step 5: Using Images from External Sources. ...
  6. Step 6: Using Images with CSS. ...
  7. Step 7: Using Images with Inline Styles.
Jan 22, 2024

How do I capture an image from a camera in react JS? ›

Taking Photos with the Camera
  1. export function usePhotoGallery() {
  2. const takePhoto = async () => {
  3. const photo = await Camera. getPhoto({
  4. resultType: CameraResultType. Uri,
  5. source: CameraSource. Camera,
  6. quality: 100,
  7. });
  8. };

How do I get the image source in react-native? ›

App.js
  1. import React, { Component } from 'react';
  2. import {StyleSheet, AppRegistry,Text, View, Image,ImageBackground } from 'react-native';
  3. export default class DisplayAnImage extends Component {
  4. render() {
  5. return (
  6. style={{width: '100%', height: '100%'}}>
  7. <Image source={require('./favicon.png')} />
  8. <Image.

How do I pass an image in react-native? ›

Share an image in a React Native app
  1. First, import the necessary modules from React Native, including the SafeAreaView, TouchableOpacity, StyleSheet, Image, Text, and View components. ...
  2. Create a new functional component called App. ...
  3. In the App component, create a new function called shareImage. ...
  4. Use the Share.
Jan 22, 2023

How do I get the image source in react native? ›

App.js
  1. import React, { Component } from 'react';
  2. import {StyleSheet, AppRegistry,Text, View, Image,ImageBackground } from 'react-native';
  3. export default class DisplayAnImage extends Component {
  4. render() {
  5. return (
  6. style={{width: '100%', height: '100%'}}>
  7. <Image source={require('./favicon.png')} />
  8. <Image.

How do I access the image from assets folder in react native? ›

React-native images can be loaded from the app's native assets catalog (drawables on android and Images. xcassets on iOS) by providing source={{uri: "image_name_without_suffix"}} . This method of loading performs much better than loading via require("../path/to/image. png") .

Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5939

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.