This app has a button to upload a local image to cloud storage. After uploading, it shows the image from the cloud using the download URL.
import React, {useState} from 'react';
import {View, Button, Image, Text} from 'react-native';
import storage from '@react-native-firebase/storage';
export default function CloudStorageExample() {
const [imageUrl, setImageUrl] = useState(null);
async function uploadAndGetUrl() {
const localPath = '/path/to/local/image.jpg';
const ref = storage().ref('uploads/myImage.jpg');
try {
await ref.putFile(localPath);
const url = await ref.getDownloadURL();
setImageUrl(url);
} catch (e) {
setImageUrl(null);
}
}
return (
<View style={{flex:1, justifyContent:'center', alignItems:'center'}}>
<Button title="Upload Image" onPress={uploadAndGetUrl} />
{imageUrl ? <Image source={{uri: imageUrl}} style={{width:200, height:200, marginTop:20}} /> : <Text>No image uploaded yet.</Text>}
</View>
);
}