import { launchCamera, launchImageLibrary, ImagePickerResponse } from 'react-native-image-picker';
const DP: React.FC = () => {
const [gallery, setGallery] = useState<string>('https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png');
const toggleComponent = () => {
Alert.alert('Choose Option', 'Select your profile', [
{
text: 'Open Camera',
onPress: () => openCamera(),
},
{
text: 'Open Gallery',
onPress: () => openGallery(),
},
{ text: 'Cancel' },
]);
};
const openGallery = async () => {
try {
const result: ImagePickerResponse = await launchImageLibrary({ mediaType: 'photo', quality: 0 });
console.log({ result });
} catch (error) {
console.error('Error opening gallery:', error);
}
};
const openCamera = async () => {
try {
const result: ImagePickerResponse = await launchCamera({ mediaType: 'photo', quality: 0 });
console.log({ result });
} catch (error) {
console.error('Error opening camera:', error);
}
};
const requestCameraPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: "App Camera Permission",
message: "App needs access to your camera",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Camera permission granted");
toggleComponent(); // Call openCamera if permission is granted
} else {
console.log("Camera permission denied");
}
} catch (err) {
console.warn(err);
}
};
return (
<View style={{ backgroundColor: "red", borderRadius: 40, width: 80,
height: 80, }}>
<View>
<Image
style={{ height: 82, width: 82, borderRadius: 50 }}
source={{ uri: gallery }}
resizeMode='cover'
/>
</View>
<TouchableOpacity onPress={requestCameraPermission} style={{marginTop: -20,
paddingLeft: 60}}>
<Entypo name="camera" size={15} color="black" />
</TouchableOpacity>
</View>
);
};