React Native : How to upload an image to Cloudinary.
Tags: reactnative, reactnativeimagepicker, cloudinary, howto
Published: 2020-03-30 01:09:23

---
title: React Native : How to upload an image to Cloudinary.
published: true
description: In this article, we will be uploading an image from a mobile camera or gallery collection to Cloudinary using React Native, We will be using hooks to manage state to display the image after it is uploaded.
tags: React Native, React Native Image Picker, Cloudinary, howto
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/hxziy3xgab6brolykyon.png
---

# Prerequisites

## Things you need to know are:

- Familiar with React Native elementary setup, if not get started here, You can also check React Native awesome documentation here.
- Basic understanding of Hooks, if not get started here.
- Little or no knowledge of Cloudinary.

## Tools Required ⚒️ 

- Node >= v13.0.0.
- React >= v16.9.0.
- React Native CLI >= v0.61.5.
- Android Studio, VSCode.
- I will be using VSCode, You can download here.
- If you are using VSCode, I use Lights Out theme by Nedy, Lovely theme.
- Cloudinary Dashboard. You can signup here, If you do not have an account.

# Introduction

In this article, we will be uploading an image from a mobile camera or gallery collection to Cloudinary using React Native, We will be using hooks to manage state to display the image after it is uploaded.

Cloudinary is an end-to-end image and video management solution for websites and mobile apps, covering everything from image and video uploads, storage, manipulations, optimizations to delivery, It provides a secure and comprehensive API for easily uploading media files from server-side code, directly from the browser or from a mobile application.

Using Cloudinary's upload capabilities, you can upload media assets in bulk with a variety of options for customizing how they will be uploaded, including naming, whether to apply manually specified or automatically generated tags and metadata, whether to apply incoming transformations or other AI-based analysis of the uploaded assets, and more.

We can look at Cloudinary as our cloud storage for media files (images, videos, PDFs, text documents, GIFs), But in this article we will be working with only images. 

# Overview on what we are building

We are building a React Native app that takes an image from a device, stores it on Cloudinary and then renders it to the screen.


![overview.gif](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585309289733_Screenshot_1585205956.png)

## Let’s get started 😊 

We have a boilerplate code, which will save us time in creating a React Native project and also setting up our screen styles, you can clone the repo  here.

# Setting up Image Picker ⚙️ 

- Let’s install React Native Image Picker :
    yarn add react-native-image-picker
- We will have to give the app some permissions to enable camera and storage rights. We will add those permissions in the AndroidManifest.xml located in android/app/main/AndroidManifest.xml. 


![AndroidManifest.xml](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585221675274_xml.png)



{% gist https://gist.github.com/godswillokokon/434d547eba0599152a3260009d753702 %}

- We will import ImagePicker from 'react-native-image-picker' in App.js
- Now we have to fix up our Image Picker, ImagePicker.showImagePicker will be needing two arguments
    1. First we have options which is an object for customizing the image picker, it can be omitted for a default options.
    2. Our second argument is a callback that sends an object in response.
- Let’s write some code in the App.js :
- From our boilerplate code we should have this :
{% gist 
 https://gist.github.com/godswillokokon/f887e833065220463ffb81ce52e3945b %}


This is some starter code that will cover UI layout and styles. 


- Now we will add our Image Picker to the code : 


{% gist 
 https://gist.github.com/godswillokokon/59b07bf7a030787cc60c8be895674561 %}


From the code above we have a function called selectPhotoTapped which bodies all our Image Picker code.

In selectPhotoTapped function we have an object called options as explained earlier, it’s an object for customizing the image picker.

Moving forward we have to access the method showImagePicker from ImagePicker, showImagePicker displays the image picker and has two arguments, options and a response object.

- After understanding how our image picker works, let’s see it work by calling the selectPhotoTapped function which will be triggered by a TouchableOpacity press event.

We will be logging out a response from both line 12 and source from line 23.
      

{% gist 
 https://gist.github.com/godswillokokon/7f5c9542a894cad323e35b65892e7b37 %}

#  Demonstration


![overview.gif](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585309289733_Screenshot_1585205956.png)

# Output :


![](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585234261481_log.png)


The data between the red lines is what we are interested in, The first log response is the entire response from Image Picker, The second log is specific data that we will be using later in future of this article it consists of three properties :

1. name
2. type
3. uri
- Now we are ready for our cloudinary setup. 🎉 🎉 🎉  

# Setting up Cloudinary ⚙️ 

Just to let us know, uploading to Cloudinary is done over HTTPS using a secure protocol based on your account's cloud_name, api_key and api_secret parameters, or using an unsigned upload without an authentication signature. When using Cloudinary's SDKs, The three security parameters are generally configured globally.


- To begin our setup we will need to create an account using this link if we do not have one already. 
- After the Signup or Sign in process, we should see our dashboard like so :


![](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585295692845_dash.png)

- We click on the settings icon at the right section of the top navbar, Then we will navigate to the upload tab we should see : 


![](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585296190315_uplo.png)

- We scroll down to where we will find Upload presets, and click Add new preset :


![](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585297854920_preset.png)

- I recommend changing Upload preset name to a custom name, and also setting Signing Mode to unsigned, this will allow us to upload to Cloudinary without an authentication signature and then save it like so :


![](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585298533936_save.png)

- Now we are done with the Cloudinary settings, Our next step, We need to create a FormData so we can upload our image and cloudinary presets.
- First, we start by setting the state for the photo, In this article, we will be using hooks.


{% gist 
 https://gist.github.com/godswillokokon/13c0fb53396c3b81661265ae980a006d %}


- Next up we are creating our Cloudinary function, we will first create a FormData and append our image file, Cloudinary presets and cloud name.


{% gist 
 https://gist.github.com/godswillokokon/e256789b0d738ccf4db090cc5c99e3e3 %}

- Next up we have to upload our data to our Cloudinary API using a fetch method. We can get our base API and cloud name here : 


![](https://paper-attachments.dropbox.com/s_03FCFC0ECCDF18A13688BB1130411C299ABC90A4CF4D82C04902E488C4300DED_1585303157765_api.png)

- Uploading our data to Cloudinary, we will be making a POST request to our base API like so :


{% gist 
 https://gist.github.com/godswillokokon/5e942ae9ae8ad4eb94aad3b15f0df6d8 %}

- From the code above we did a POST request to our base API and from the response data we will be making use of only secure_url  and we updated the state of our photo.
- Now we have our function set up, In the selectPhotoTapped function, we will be calling cloudinaryUpload and passing our image data as an argument.


{% gist 
 https://gist.github.com/godswillokokon/0411463d58f4d3d2958710f37dec0ff1 %}


# Conclusion

Finally we can take a look at our entire code and have a rundown on the steps.


1. We set up our UI layout.
2. We installed Image Picker from React Native.
3. We set up our Image Picker and tested it.
4. We set up our Cloudinary presets.
5. We fix up our Cloudinary in our code by sending a request to our base API.
6. We got the response and use it to update our image display.

Here is the complete code, feel free to send a pull request if you like to add something to what we have.

# Thanks for reading ❤️ 

Hope this helps, you can tweet at @Godswillokokon if you have any questions.