I want to use react-native-camera in expo but I get this error.-> Possible Unhandled Promise Rejection (id: 0) Here is code.
import {StatusBar} from 'expo-status-bar';
import React from "react";
import { StyleSheet, Modal, View, Pressable, Text, SafeAreaView, Button, Image,
PushNotificationIOS } from "react-native";
import { useEffect, useRef, useState } from 'react';
import Icon from "react-native-vector-icons/MaterialIcons";
import {shareAsync} from 'expo-sharing';
import * as MediaLibrary from 'expo-media-library';
import { Camera } from 'expo-camera';
export default function App() {
let cameraRef = useRef();
const [hasCameraPermission, setHasCameraPermission] = useState();
const [hasMediaLibraryPermission, setHasMediaLibraryPermission] = useState();
const [photo, setPhoto] = useState();
useEffect(()=>{
(async ()=>{
const cameraPermission = await Camera.requestCameraPermissionsAsync();
constMediaLibraryPermission = await MediaLibrary.requestMediaLibraryPermissionsAsync();
setHasCameraPermission(cameraPermission.status === "granted");
setHasMediaLibraryPermission(MediaLibraryPermission.status === "granted");
})();
},[]);
if (hasCameraPermission === undefined){
return <Text> Requesting permissions...</Text>
} else if (!hasCameraPermission){
return <Text> Permission for camera not granted. Please change this in settinr.</Text>
}
let takePic = async () => {
let options = {
quality :1,
base64: true,
exif : false
};
let newPhoto = await cameraRef.current.takePictureAsync(options);
setPhoto(newPhoto);
};
if(photo){
let sharePic = () => {
shareAsync(photo.uri).then(() => {
setPhoto(undefined);
});
};
let savePhoto = () => {
MediaLibrary.saveToLibraryAsync(photo.uri).then(() =>{
setPhoto(undefined);
});
};
return (
<SafeAreaView style = {styles.container}>
<Image style = {styles.preview} source = {{uri: "data:image/jpg;base64," +
photo.base64}}/>
<Button title = "share" onPress = {sharePic}/>
{hasMediaLibraryPermission ? <Button title = "save" onPress = {savePhoto}/>:undefined}
<Button title = "Discard" onPress = {()=> setPhoto(undefined)}/>
</SafeAreaView>
)
}
return (
<Camera style={styles.container} ref = {cameraRef}>
<View style = {styles.buttonContainer}>
<Button title = "Takc Pic" onPress = {takePic} />
</View>
<StatusBar style="auto" />
</Camera>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
buttonContainer:{
backgroundColor :'#fff',
alignSelf:'flex-end'
},
preview:{
alignSelf:'stretch',
flex:1
}
});
So I try 'catch-throw', but I didn't solve this problem. Because 'try' is necessary.
I checked other questions and answers, but I didn't find a solution.
How can I solve this error?