Request missing write to storage permission to use the camera on Android 9 and below (#7292)

This commit is contained in:
Elias Nahum 2023-04-18 13:54:22 -04:00 committed by GitHub
parent f50144b15d
commit c3ed9d3a4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -223,6 +223,42 @@ export default class FilePickerUtil {
return true;
};
private hasWriteStoragePermission = async () => {
if (Platform.OS === 'android' && Platform.Version <= 28) {
const storagePermission = Permissions.PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE;
let permissionRequest;
const hasPermissionToStorage = await Permissions.check(storagePermission);
switch (hasPermissionToStorage) {
case Permissions.RESULTS.DENIED:
permissionRequest = await Permissions.request(storagePermission);
return permissionRequest === Permissions.RESULTS.GRANTED;
case Permissions.RESULTS.BLOCKED: {
const {title, text} = this.getPermissionDeniedMessage('storage');
Alert.alert(title, text, [
{
text: this.intl.formatMessage({
id: 'mobile.permission_denied_dismiss',
defaultMessage: "Don't Allow",
}),
},
{
text: this.intl.formatMessage({
id: 'mobile.permission_denied_retry',
defaultMessage: 'Settings',
}),
onPress: () => AndroidOpenSettings.appDetailsSettings(),
},
]);
return false;
}
default: return true;
}
}
return true;
};
private buildUri = async (doc: DocumentPickerResponse) => {
let uri: string = doc.uri;
@ -256,7 +292,13 @@ export default class FilePickerUtil {
}
const hasCameraPermission = await this.hasPhotoPermission('camera');
if (hasCameraPermission) {
let hasWriteToStoragePermission = true;
if (Platform.OS === 'android' && Platform.Version <= 28) {
hasWriteToStoragePermission = await this.hasWriteStoragePermission();
}
if (hasCameraPermission && hasWriteToStoragePermission) {
launchCamera(options, async (response: ImagePickerResponse) => {
StatusBar.setHidden(false);