How to turn a gallery image into BASE64

Asked

Viewed 13 times

-1

I have a function that takes the image from the gallery and the camera and sends it to an area where the user can view it.

    import React, {useState} from 'react';
    import {View, TouchableOpacity, Text, StyleSheet, Image} from 'react-native';
    
    import ImagePicker from 'react-native-image-picker';
    
    export default function Upload() {
      const [avatar, setAvatar] = useState();
    
      function imagePickerCallback(data) {
        if (data.didCancel) {
          return;
        }
    
        if (data.error) {
          return;
        }
    
        if (!data.assets[0].uri) {
          return;
        }
    
        setAvatar(data.assets[0]);
      }
    
      return (
        <View style={styles.container}>
          <Image
            source={{
              uri: avatar
                ? avatar.uri
                : 'https://mltmpgeox6sf.i.optimole.com/w:761/h:720/q:auto/https://redbanksmilesnj.com/wp-content/uploads/2015/11/man-avatar-placeholder.png',
            }}
            style={styles.avatar}
          />
          <TouchableOpacity
            style={styles.button}
            onPress={() =>
              ImagePicker.launchCamera(imagePickerCallback)
            }>
            <Text style={styles.buttonText}>camera</Text>
          </TouchableOpacity>
    
          <TouchableOpacity
            style={styles.button}
            onPress={() =>
              ImagePicker.launchImageLibrary(imagePickerCallback)
            }>
            <Text style={styles.buttonText}>galery</Text>
          </TouchableOpacity>
    
        </View>
      );
    }

I am filling the source of the image with the URI obtained when picking up the image from the gallery or when taking a photo with the camera. My question is, how to turn this image into BASE64 to send it to firebase?

If necessary to install some library, please inform.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.