How to Use React-Native-Image-Picker

How to select media from the gallery or camera

Gapur Kassym
JavaScript in Plain English
4 min readSep 3, 2021

Photo by Lilly Rum on Unsplash

Picking images from gallery or camera is one of the most popular and basic tasks to develop an app with React native. How we can do that?

Today I am back to talk about how we can easily select images from your device’s library or use camera to capture photo. Let me introduce you to React Native Image Picker.

In this article, I will show React Native Image Picker by developing an app with uploading images. The app will enable us to pick and display photo.

Setting Up the Project

Before we get started, I need to create a new React Native project with the following lines of code:

react-native init react_native_image_avatar_picker
cd react_native_image_avatar_picker
npm run ios

Awesome, we’ve successfully created our React Native app.

Now, I am going to use React Native Image Picker library to implement image picker component. It is a React Native module that allows you to select a photo/video from the device library or camera. Let’s install it with the following commands:

yarn add react-native-image-picker
cd ios && pod install && cd ..

Next, we need to add iOS permissions to our app Info.plist:

<key>NSPhotoLibraryUsageDescription</key>
<string>Your message to user when the photo library is accessed for the first time</string>
<key>NSCameraUsageDescription</key>
<string>The camera is accessed for the first time</string>
<key>NSMicrophoneUsageDescription</key>
<string>The microphone is accessed for the first time</string>

We don’t require permissions for Android(saveToPhotos requires permission check).

After that, if we run the app and everything is fine, then we are ready to code!

Create A Base App Screen

The basic idea is to build an image picker component that will allow the user to upload a new photo from the device’s library or capture the photo via the camera.

First of all, we will update a base app screen with a new green header and background. Let’s create a ImagePickerHeader component:

Next, I’m going to create a main ImagePickerAvatar component that allows you to upload images.

Here is the result:

Main Image Avatar Picker Screen

Pick A Photo From Device

In this tutorial, We are going to implement two ability to select or capture a photo via camera. I will use a customizable, animated modal using the React Native Modal package to show the user two options. For this, we need to install it:

yarn add react-native-modal

Next, I will create an ImagePickerModal component:

Last but not least, We need to implement two functions:

  • onImageLibraryPress — select image from library
  • onCameraPress — take a photo using the camera

Now, Let’s update our App.js file:

Let’s Demo Our Image Picker App

React-Native-Image-Avatar-Picker

If you want to check all the code, here’s the link to Github.

Thanks for reading, I hope you found this piece useful. Happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Written by Gapur Kassym

Senior Software Engineer at Twilio, Technical Writer #JavaScript #React #React-Native #Nodejs. Follow me on twitter https://twitter.com/GKassym

Responses (4)

Write a response

Thanks, mister!!

3

What a wonderful tutorial Gapur !
Very very thank you for this .

3

Does it work well on Android? On my Android device I had the selfie and the photo was rotated by 90 degrees clockwise. There was a discussion here: https://github.com/react-native-image-picker/react-native-image-picker/issues/655, but the solution…

2