React Native

Save Files from a React Native App

Introduction

In this guide, you will learn how to store and retrieve files in your React Native Application using Parse Javascript SDK to manage Back4app cloud storage.

In the Parse world, we use the type Parse.File to manage files. After creating the Parse.File, you will store it on the Back4App Cloud using the save() method. You should always associate the file with another data object so you can retrieve this file path when querying the object. If you do not associate, the file will be stored, but you will not find them on the Cloud.

Another important tip is to give a name to the file that has a file extension. This extension lets Parse figure out the file type and handle it accordingly. We should also mention that Each upload gets a unique identifier, so there’s no problem with uploading multiple files using the same name.

React Native App’s most common use case is storing images. In this guide, you will build a demo gallery app to store and display pictures.

The full sample code for the created App in this tutorial is here. Feel free to follow along step by step or jump straight to the code.
playing images.

  • If you do not associate your file to a data object the file will become an orphan file and you wont be able to find it on Back4App Cloud.

Goal

To create a React Native gallery App that uploads and displays images using Parse Javascript and Back4app.

Prerequisites

To complete this tutorial, you will need:

Step 1 - Installing dependencies

Working with files (i.e., uploading photos) on React Native apps is one of the most common features. In this tutorial, you will build a simple gallery App that uploads and displays images.

Once you have a React Native project successfully connected with Back4app, go to its root directory and install the following dependency:

cd startWithBack4app

# To select images on devices
yarn add react-native-image-picker 

For iOS, install pods:

cd ios && npx pod-install

Note that auto-linking is available for React native v0.60+, but for information on installing react-native-image-picker older versions, check the official documentation here.

After installing, you will need to add the NSPhotoLibraryUsageDescription key to your info.plist for allowing the user to select image/video from photos on iOS.

<dict>
//other keys ...
<key>NSPhotoLibraryUsageDescription</key>
<string>APP_NAME_HERE would like access to your photo</string> 
</dict>

On android no permissions are required to select photos for gallery.

Next, you will build a component that wraps the UI and logic for selecting an image from the gallery and uploading it.

In your root directory, create a UploadingImage.js file with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import  React, {useState} from  'react';
import {View, Button, Image, StyleSheet} from  'react-native';

import {launchImageLibrary} from  'react-native-image-picker';
import Parse from 'parse/react-native.js';

const  UploadImage = () => {
const [image, setImage] = useState(null);

async function upload() {
	// TODO: implement this method
}
// This will open phone image library
function pickImage() {
  launchImageLibrary(
	{
	  mediaType:  'photo',
	  includeBase64:  true,
	  maxHeight:  200,
	  maxWidth:  200,
	},
	(response) => {
	  // Add selected image to the state
	  setImage(response);
	},
  );
}

return (
  <View>
	<Button
	  onPress={pickImage}
	  title="Pick an image from gallery"
	  color="#841584" />
	  {image && <Image source={ {uri: image.uri} } style={styles.currentImage}/>}

	  {image && <Button title="Upload" color="green" onPress={upload}  />}
  </View>
);

};

const styles = StyleSheet.create({
  container: {
    height:  400,
    justifyContent:  'center',
    alignItems:  'center',
  },
  currentImage: {
	width:  250,
	height:  250,
	resizeMode:  'cover',
	alignSelf:  'center',
  },
});

export  default  UploadImage;

The above component renders:

  1. A button which opens the image library when a user clicks
  2. The selected image along with an upload button

As you can see, the upload method does not do anything. Next, you will implement its behavior and see how to actually upload images to Back4app cloud.

Step 3 - Uploading an Image

Back4app storage is built upon Parse.File and lets you store any files such as documents, images, videos, music, and any other binary data. Parse.File is a utility class that Parse Javascript SDK provides to abstract the file storage process and make it easy for you.

Therefore, to upload an image, you will only need to create a Parse.File instance and then call the save method. By doing this, Parse will automatically do the rest for you. You can read the full documentation about Parse Files here.

Let’s do that in our upload function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
async function upload() {
  // 1. Create a file
  const {base64, fileName} = image;
  const  parseFile = new  Parse.File(fileName, {base64});

  // 2. Save the file
  try {
	const responseFile = await  parseFile.save();
	const Gallery = Parse.Object.extend('Gallery');
	const gallery = new  Gallery();
	gallery.set('picture', responseFile);

	await gallery.save();
	Alert.alert('The file has been saved to Back4app.');
  } catch (error) {
	  console.log(
	    'The file either could not be read, or could not be saved to Back4app.',
	  );
	}
}

In short, the above snippet creates and saves the selected image, and after the save completes, we associate it with a Parse.Object called Gallery.

Now you need to import and use the UploadImage component in your App.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import React from 'react';
import {SafeAreaView, StyleSheet} from 'react-native';
// In a React Native application
import Parse from 'parse/react-native.js';
import AsyncStorage from '@react-native-community/async-storage';
import keys from './constants/Keys';
//Before using the SDK...
Parse.setAsyncStorage(AsyncStorage);
Parse.initialize(keys.applicationId, keys.javascriptKey);
Parse.serverURL = keys.serverURL;

import UploadImage from './UploadImage';

const App = () => {
  return (
	<SafeAreaView style={styles.container}>
		<UploadImage/>
	</SafeAreaView>
  )
};

const styles = StyleSheet.create({
  container: {
	backgroundColor: '#f5f5f5',
	flex: 1,
  },
  title: {
	fontSize: 18,
	textAlign: 'center',
	fontWeight: 'bold',
  },
});

export default App;

Once you do that, you should be able to pick images from the gallery:

React Native Back4App

And successfully upload images hitting the upload button:

React Native Back4App

Step 4 - Displaying Images

We need to get the image’s URL to retrieve the image’s contents and display it to our users. Next, you will build a component for querying images from our Gallery Object and displaying them on a FlatList.

In your root directory, create a Gallery.js file with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, {useState, useEffect} from  'react';
import {Text, Image, FlatList, StyleSheet} from 'react-native';

import Parse from 'parse/react-native.js';

const Gallery = () => {
const [images, setImages] = useState([]);

useEffect(() => {
  const fetchImages = async () => {
	let query = new Parse.Query('Gallery');
	const results = await query.find();
	setImages(results);
  };
  
  fetchImages();
}, []);

return (
  <FlatList
    style={styles.container}
	contentContainerStyle={styles.listContent}
	data={images}
	horizontal={false}
	numColumns={3}
	ListEmptyComponent={() =>  <Text>No images uploaded.</Text>}
	renderItem={({item}) => 
	  <Image source={ {uri: item.get('picture').url()} } style={styles.imageItem}/>
	)}
	keyExtractor={(item) => item.id}
  />);
};

const  styles = StyleSheet.create({
  container: {
	flex: 1,
	backgroundColor: '#f5f5f5',
  },
  listContent: {
	justifyContent: 'center',
	alignItems: 'center',
  },
  imageItem: {
	width: 100,
	height: 100,
	resizeMode: 'cover',
	marginHorizontal: 5,
	marginVertical: 5,
  },
});

export default Gallery;

The above component uses the useEffect hook to query images uploaded to the Gallery Object once it finishes rendering. Next, you will need to add the component to your App.js:

1
2
3
4
5
6
7
8
9
10
11
12
// ...other imports
import UploadImage from './UploadImage';
import Gallery from './Gallery';

const App = () => {
  return (
	<SafeAreaView style={styles.container}>
	  <UploadImage/>
	  <Gallery/>
	</SafeAreaView>
	);
}

When you run your App, you should be able to see the list of uploaded images like this:

React Native Back4App

Step 5 - It’s Done!

At this point, you have uploaded your first image on Back4App and displayed it in a React Native application.