Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then((response) => {
const { status, expires, permissions } = response
if (status === 'granted') {
ImagePicker.launchImageLibraryAsync({
// mediaTypes: 'All',
base64: true,
quality: 1,
})
.then((response) => {
if (!response.cancelled) {
const { user, sendImage } = this.props
//TODO: Check if it is a video and store video
const data = { uri: response.uri, user }
sendImage(data)
}
})
.catch((error) => console.log(error))
}
})
.catch((error) => console.log(error))
.then((response) => {
const { status } = response
if (status === 'granted') {
ImagePicker.launchImageLibraryAsync({
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))
}
})
.catch((error) => console.log(error))
.then((response) => {
const { status, expires, permissions } = response
if (status === 'granted') {
ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [1, 1],
quality: 0.5,
})
.then((response) => {
console.log(response)
if (!response.cancelled) {
this.props.uploadImage(response.uri)
}
})
.catch((error) => console.log(error))
}
})
.catch((error) => console.log(error))
pickImage = async ({ onSend }) => {
const { t } = this.props;
if (await this.checkPermission(Permissions.CAMERA_ROLL, 'android')) {
const { cancelled, uri } = await ImagePicker.launchImageLibraryAsync(settings.chat.image.imagePicker);
if (!cancelled) {
const { size } = await FileSystem.getInfoAsync(uri);
const reg = /[^\\/]*\.\w+$/;
if (size <= settings.chat.image.maxSize && reg.test(uri)) {
const type = mime.lookup(uri);
const name = uri.match(reg)[0];
const imageData = new ReactNativeFile({ uri, type, name });
onSend({ attachment: imageData });
} else {
this.setState({ notify: t('attachment.errorMsg') });
}
}
} else {
this.setState({ notify: t('permission.errorMsg') });
}
};
export async function pickImageAsync(onSend) {
if (await getPermissionAsync(Permissions.CAMERA_ROLL)) {
const result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
})
if (!result.cancelled) {
onSend([{ image: result.uri }])
return result.uri
}
}
}
export const launchImageLibraryAsync = (
mediaType: 'photo' | 'video' | 'mixed',
askPermAndRetry: boolean = true
): Promise => {
return ImagePicker.launchImageLibraryAsync({mediaTypes: mediaTypeToImagePickerMediaType(mediaType)}).catch(
retyAfterAskingPerm(
[Permissions.CAMERA_ROLL],
askPermAndRetry ? () => launchImageLibraryAsync(mediaType, false) : null
)
)
}
_selectPhoto = async () => {
const status = await getPermission(Permissions.CAMERA_ROLL);
if (status) {
const result = await ImagePicker.launchImageLibraryAsync(options);
if (!result.cancelled) {
this.props.navigation.navigate("NewPost", { image: result.uri });
}
}
};
export async function choosePhotoAsync(): Promise {
if (!(await ensurePermissionsAsync())) return null;
const media = await ImagePicker.launchImageLibraryAsync(mediaOptions);
if (!media.cancelled) {
return await moveToPhotoCacheAsync(media.uri);
}
return null;
}