From c3ed9d3a4a41006a96a428e39eaf3422d261c7a0 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Tue, 18 Apr 2023 13:54:22 -0400 Subject: [PATCH] Request missing write to storage permission to use the camera on Android 9 and below (#7292) --- app/utils/file/file_picker/index.ts | 44 ++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/app/utils/file/file_picker/index.ts b/app/utils/file/file_picker/index.ts index 1e268c673..fa6289a0f 100644 --- a/app/utils/file/file_picker/index.ts +++ b/app/utils/file/file_picker/index.ts @@ -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);