finish methods of pick images and videos
This commit is contained in:
parent
70e6ae40cf
commit
7ab2727946
|
@ -5,58 +5,40 @@ import { PermissionsAndroid } from 'react-native';
|
||||||
import { isAndroid } from './helpers';
|
import { isAndroid } from './helpers';
|
||||||
import log from './helpers/log';
|
import log from './helpers/log';
|
||||||
|
|
||||||
export const pickImage = async (editImage = true): Promise<ImagePicker.ImagePickerResult | null> => {
|
const handlePermission = async (): Promise<boolean> => {
|
||||||
try {
|
|
||||||
const result = await ImagePicker.launchImageLibraryAsync({
|
|
||||||
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
|
||||||
allowsEditing: editImage,
|
|
||||||
quality: 0
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
log(error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const pickVideo = async (editImage = true): Promise<ImagePicker.ImagePickerResult | null> => {
|
|
||||||
try {
|
|
||||||
const result = await ImagePicker.launchImageLibraryAsync({
|
|
||||||
mediaTypes: ImagePicker.MediaTypeOptions.Videos,
|
|
||||||
allowsEditing: editImage,
|
|
||||||
quality: 0
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
log(error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const pickImageFromCamera = async (allowsEditing = false): Promise<ImagePicker.ImagePickerResult | null> => {
|
|
||||||
try {
|
|
||||||
if (isAndroid) {
|
if (isAndroid) {
|
||||||
const permissions = await PermissionsAndroid.requestMultiple([
|
const permissions = await PermissionsAndroid.requestMultiple([
|
||||||
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
|
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
|
||||||
PermissionsAndroid.PERMISSIONS.CAMERA
|
PermissionsAndroid.PERMISSIONS.CAMERA
|
||||||
]);
|
]);
|
||||||
if (permissions['android.permission.CAMERA'] !== 'granted') {
|
if (permissions['android.permission.CAMERA'] !== 'granted') {
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const permission = await ImagePicker.requestCameraPermissionsAsync();
|
const permission = await ImagePicker.requestCameraPermissionsAsync();
|
||||||
if (!permission.granted) {
|
if (!permission.granted) {
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const permission = await ImagePicker.getCameraPermissionsAsync();
|
const permission = await ImagePicker.getCameraPermissionsAsync();
|
||||||
if (!permission.granted) {
|
if (!permission.granted) {
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const pickFromCamera = async (
|
||||||
|
allowsEditing: boolean,
|
||||||
|
mediaType: ImagePicker.MediaTypeOptions,
|
||||||
|
mime: string
|
||||||
|
): Promise<ImagePicker.ImagePickerResult | null> => {
|
||||||
|
try {
|
||||||
|
const hasPermission = await handlePermission();
|
||||||
|
if (!hasPermission) return null;
|
||||||
const result = await ImagePicker.launchCameraAsync({
|
const result = await ImagePicker.launchCameraAsync({
|
||||||
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
mediaTypes: mediaType,
|
||||||
quality: 1,
|
quality: 1,
|
||||||
allowsEditing
|
allowsEditing
|
||||||
});
|
});
|
||||||
|
@ -68,7 +50,7 @@ export const pickImageFromCamera = async (allowsEditing = false): Promise<ImageP
|
||||||
path: result.uri,
|
path: result.uri,
|
||||||
filename: `${file.uri.substring(file.uri.lastIndexOf('/') + 1)}`,
|
filename: `${file.uri.substring(file.uri.lastIndexOf('/') + 1)}`,
|
||||||
size: file.size,
|
size: file.size,
|
||||||
mime: 'image/jpeg'
|
mime
|
||||||
};
|
};
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
@ -78,3 +60,26 @@ export const pickImageFromCamera = async (allowsEditing = false): Promise<ImageP
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const pickImageAndVideoFromLibrary = async (): Promise<ImagePicker.ImagePickerMultipleResult | null> => {
|
||||||
|
try {
|
||||||
|
const result = await ImagePicker.launchImageLibraryAsync({
|
||||||
|
mediaTypes: ImagePicker.MediaTypeOptions.All,
|
||||||
|
quality: undefined, // to force animated gifs
|
||||||
|
allowsMultipleSelection: true
|
||||||
|
});
|
||||||
|
if (!result.cancelled) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
log(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const pickVideoFromCamera = (allowsEditing = false): Promise<ImagePicker.ImagePickerResult | null> =>
|
||||||
|
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Videos, 'video/mp4');
|
||||||
|
|
||||||
|
export const pickImageFromCamera = (allowsEditing = false): Promise<ImagePicker.ImagePickerResult | null> =>
|
||||||
|
pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Images, 'image/jpeg');
|
||||||
|
|
Loading…
Reference in New Issue