Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
handleTakeImage = () => {
Permissions.askAsync(Permissions.CAMERA_ROLL, Permissions.CAMERA)
.then((response) => {
const { status } = response
if (status === 'granted') {
ImagePicker.launchCameraAsync({
allowsEditing: true,
aspect: [1, 1],
base64: true,
quality: 0.5,
})
.then((response) => {
if (!response.cancelled) {
this.props.uploadImage(response.uri)
}
})
.catch((error) => console.log(error))
}
renderSinglePermissionsButtons() {
const permissions = [
['CAMERA', Permissions.CAMERA],
['AUDIO_RECORDING', Permissions.AUDIO_RECORDING],
['LOCATION', Permissions.LOCATION],
['USER_FACING_NOTIFICATIONS', Permissions.USER_FACING_NOTIFICATIONS],
['NOTIFICATIONS', Permissions.NOTIFICATIONS],
['CONTACTS', Permissions.CONTACTS],
['SYSTEM_BRIGHTNESS', Permissions.SYSTEM_BRIGHTNESS],
['CAMERA_ROLL', Permissions.CAMERA_ROLL],
['CALENDAR', Permissions.CALENDAR],
['REMINDERS', Permissions.REMINDERS],
];
return permissions.map(([permissionName, permissionType]) => (
async function ensurePermissionsAsync(): Promise {
const { status } = await Permissions.askAsync(Permissions.CAMERA, Permissions.CAMERA_ROLL);
if (status !== Permissions.PermissionStatus.GRANTED) {
alert(
'Cannot select a banner photo without media access! Please enable the "Camera" & "Camera Roll" permission in your system settings.'
);
return false;
}
return true;
}
private onAddButtonPress = (): void => {
Permissions.askAsync(Permissions.CAMERA_ROLL)
.then(this.onCameraPermissionResponse);
};
const openImagePickerModal = async () => {
const cameraRollPermissions = await Permissions.askAsync(Permissions.CAMERA_ROLL);
const hasCameraRollPermission = cameraRollPermissions.status === PERMISSIONS_STATUS.GRANTED;
const cameraPermissions = await Permissions.askAsync(Permissions.CAMERA);
const hasCameraPermission = cameraPermissions.status === PERMISSIONS_STATUS.GRANTED;
toggleImagePicker(hasCameraPermission && hasCameraRollPermission);
togglePermissionsModal(!(hasCameraPermission && hasCameraRollPermission));
};
_pickPhoto = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
alert('Permission to CAMERA_ROLL not granted!');
return;
}
const result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: false,
});
if (result.cancelled) {
alert('No image selected!');
return;
}
this.setState({ image: result });
}
renderSinglePermissionsButtons() {
const permissions: Array<[string, Permissions.PermissionType]> = [
['CAMERA', Permissions.CAMERA],
['AUDIO_RECORDING', Permissions.AUDIO_RECORDING],
['LOCATION', Permissions.LOCATION],
['USER_FACING_NOTIFICATIONS', Permissions.USER_FACING_NOTIFICATIONS],
['NOTIFICATIONS', Permissions.NOTIFICATIONS],
['CONTACTS', Permissions.CONTACTS],
['SYSTEM_BRIGHTNESS', Permissions.SYSTEM_BRIGHTNESS],
['CAMERA_ROLL', Permissions.CAMERA_ROLL],
['CALENDAR', Permissions.CALENDAR],
['REMINDERS', Permissions.REMINDERS],
];
return permissions.map(([permissionName, permissionType]) => (
_pickImage = async () => {
await this._askPermission(
Permissions.CAMERA_ROLL,
'We need the camera-roll permission to read pictures from your phone...'
);
let pickerResult = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
this._handleImagePicked(pickerResult);
};
saveToGallery = async () => {
const photos = this.state.selected;
if (photos.length > 0) {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
throw new Error('Denied CAMERA_ROLL permissions!');
}
const promises = photos.map(photoUri => {
return MediaLibrary.createAssetAsync(photoUri);
});
await Promise.all(promises);
alert('Successfully saved photos to user\'s gallery!');
} else {
alert('No photos to save!');
}
}
saveToGallery = async () => {
const photos = this.state.selected;
if (photos.length > 0) {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
throw new Error('Denied CAMERA_ROLL permissions!');
}
const promises = photos.map(photoUri => {
return MediaLibrary.createAssetAsync(photoUri);
});
await Promise.all(promises);
alert('Successfully saved photos to user\'s gallery!');
} else {
alert('No photos to save!');
}
}