Soy nuevo en reaccionar nativo y estoy practicando la conversión de imágenes en archivos binarios. Descargué al azar la imagen de Internet. Estoy tratando de convertir imágenes en archivos binarios para cargarlos en el servidor a través de la API. El servidor toma la entrada como un archivo binario. He adjuntado el código a continuación. Busque en internet pero no encontré la solución.
import React from 'react' import { View, Text, StyleSheet, Image, Button, Alert } from 'react-native' let imagePath = './src/image/macTest.jpeg' const imageToBinary = (imagePath) => { //Alert.alert(imagePath) // this function needs to convert image into the binary file } const App = () => { return ( <View> <Text style={styles.headerStyle}> Image </Text> <Image style={styles.imageStyle} source= {require('./src/image/macTest.jpeg')} /> <Button title="Binary Value" onPress={() => imageToBinary(imagePath)} /> <View style= {styles.binaryStyle}> <Text> // binary value of the image </Text> </View> </View> ) } const styles = StyleSheet.create({ headerStyle:{ fontSize: 20, textAlign: 'center', margin: 15 }, imageStyle:{ alignItems: 'center', height: 300, width: 450, }, binaryStyle: { borderWidth: 1, borderColor: 'black', width: 400, height: 250, margin: 5 } }) export default App;Si está utilizando Expo, hay FileSystem :
https://docs.expo.dev/versions/latest/sdk/filesystem/#filesystemreadasstringasyncfileuri-options
Ejemplo:
const img = await FileSystem.readAsStringAsync(photo.uri, { encoding: 'base64' }); Otra alternativa es react-native-image-picker :