Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if camera permission cannot be requested go to settings #2299

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions package/expo-package/src/handlers/takePhoto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Image, Platform } from 'react-native';
import { Image, Linking, Platform } from 'react-native';

import * as ImagePicker from 'expo-image-picker';

Expand All @@ -10,12 +10,19 @@ type Size = {
export const takePhoto = async ({ compressImageQuality = 1 }) => {
try {
const permissionCheck = await ImagePicker.getCameraPermissionsAsync();
const permissionGranted =
permissionCheck?.status === 'granted'
? permissionCheck
: await ImagePicker.requestCameraPermissionsAsync();
const canRequest = permissionCheck.canAskAgain;
let permissionGranted = permissionCheck.granted;
if (!permissionGranted) {
if (canRequest) {
const response = await ImagePicker.requestCameraPermissionsAsync();
permissionGranted = response.granted;
} else {
Linking.openSettings();
return { cancelled: true };
}
}

if (permissionGranted?.status === 'granted' || permissionGranted?.granted === true) {
if (permissionGranted) {
const imagePickerSuccessResult = await ImagePicker.launchCameraAsync({
quality: Math.min(Math.max(0, compressImageQuality), 1),
});
Expand Down
97 changes: 61 additions & 36 deletions package/native-package/src/handlers/takePhoto.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,72 @@
import { Image, Platform } from 'react-native';
import { AppState, Image, Linking, PermissionsAndroid, Platform } from 'react-native';
import ImagePicker from 'react-native-image-crop-picker';

export const takePhoto = async ({ compressImageQuality = Platform.OS === 'ios' ? 0.8 : 1 }) => {
const photo = await ImagePicker.openCamera({
compressImageQuality: Math.min(Math.max(0, compressImageQuality), 1),
});
let hadDeniedAndroidPermission = false;

if (photo.height && photo.width && photo.path) {
let size: { height?: number; width?: number } = {};
if (Platform.OS === 'android') {
// Height and width returned by ImagePicker are incorrect on Android.
// The issue is described in following github issue:
// https://github.com/ivpusic/react-native-image-crop-picker/issues/901
// This we can't rely on them as it is, and we need to use Image.getSize
// to get accurate size.
const getSize = (): Promise<{ height: number; width: number }> =>
new Promise((resolve) => {
Image.getSize(photo.path, (width, height) => {
resolve({ height, width });
export const takePhoto = async ({ compressImageQuality = Platform.OS === 'ios' ? 0.8 : 1 }) => {
if (Platform.OS === 'android') {
const cameraPermissions = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA);
if (!cameraPermissions) {
const androidPermissionStatus = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
);
if (androidPermissionStatus === PermissionsAndroid.RESULTS.DENIED) {
hadDeniedAndroidPermission = true;
return { cancelled: true };
} else if (androidPermissionStatus === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
if (!hadDeniedAndroidPermission) {
Linking.openSettings();
}
return { cancelled: true };
}
}
}
try {
const photo = await ImagePicker.openCamera({
compressImageQuality: Math.min(Math.max(0, compressImageQuality), 1),
});
if (photo.height && photo.width && photo.path) {
let size: { height?: number; width?: number } = {};
if (Platform.OS === 'android') {
// Height and width returned by ImagePicker are incorrect on Android.
// The issue is described in following github issue:
// https://github.com/ivpusic/react-native-image-crop-picker/issues/901
// This we can't rely on them as it is, and we need to use Image.getSize
// to get accurate size.
const getSize = (): Promise<{ height: number; width: number }> =>
new Promise((resolve) => {
Image.getSize(photo.path, (width, height) => {
resolve({ height, width });
});
});
});

try {
const { height, width } = await getSize();
size.height = height;
size.width = width;
} catch (e) {
// do nothing
console.warn('Error get image size of picture caputred from camera ', e);
try {
const { height, width } = await getSize();
size.height = height;
size.width = width;
} catch (e) {
// do nothing
console.warn('Error get image size of picture caputred from camera ', e);
}
} else {
size = {
height: photo.height,
width: photo.width,
};
}
} else {
size = {
height: photo.height,
width: photo.width,
return {
cancelled: false,
source: 'camera',
uri: photo.path,
...size,
};
}

return {
cancelled: false,
source: 'camera',
uri: photo.path,
...size,
};
} catch (e: unknown) {
// on iOS: if it was in inactive state, then the user had just denied the permissions
if (Platform.OS === 'ios' && AppState.currentState === 'active') {
await Linking.openSettings();
}
}

return { cancelled: true };
};