skip to Main Content

On a specific device (Samsung Galaxy S9 with Android 9), when I try to open the camera through ExponentImagePicker, I get the following error:

Error: Call to function 'ExponentImagePicker.launchCameraAsync' has been rejected.
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property cameraLauncher has not been initialized

On an Android 9 emulator it works, and also for newer API version emulators.
This was working previously, but seems to have stopped working after updating react native and other libraries.

Anything I can do about it?

Code:

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

const MediaSelector: React.FC<Props> = (props) => {
    const open = async () => {
        const permissions = await ImagePicker.requestCameraPermissionsAsync();
        if (!permissions.granted) return Alert.alert("permission denied!"))
        
        const config: ImagePicker.ImagePickerOptions = {
            mediaTypes: ImagePicker.MediaTypeOptions.Images,
            allowsEditing: true,
            allowsMultipleSelection: false,
            exif: false,
            aspect: [1, 1],
        }
        try {
            const result = await ImagePicker.launchCameraAsync(config);
        } catch (error) {
            console.log(error)
            Alert.alert("error!")
            return
        }
    }
    
    return <Pressable style={styles.container} onPress={open}>
        <ImageView img={props.image}/>
    </Pressable/>
}

versions:

"react": "18.0.0",
"expo-image-picker": "~13.3.1",
"react-native": "0.69.6",

2

Answers


  1. I had the same issue, and for some reason using getCameraPermissionsAsync() fixed the issue – whereas requestCameraPermissionsAsync() on its own would cause launchCameraAsync() to be rejected on Android devices.

    See the following:

    let permissionResult = await ImagePicker.getCameraPermissionsAsync();
            if(permissionResult.status !== 'granted') {
                permissionResult = await ImagePicker.requestCameraPermissionsAsync();
            }
            if(permissionResult.status !== 'granted') {
                alert("You must turn on camera permissions to record a video.");
            }
            else {
                let result = await ImagePicker.launchCameraAsync({
                    mediaTypes: ImagePicker.MediaTypeOptions.Videos,
                    allowsEditing: true,
                    aspect: [3, 4],
                });
    
    
    Login or Signup to reply.
  2. i commented ensureCameraPermissionsAreGranted() on node_modules/expo-image-picker/android/src/main/java/expo/module/imagepicker/ImagePickerModule.kt

        AsyncFunction("launchCameraAsync") Coroutine { options: ImagePickerOptions ->
              //ensureTargetActivityIsAvailable(options)
              //ensureCameraPermissionsAreGranted()
    
          val mediaFile = createOutputFile(context.cacheDir, options.mediaTypes.toFileExtension())
          val uri = mediaFile.toContentUri(context)
          val contractOptions = options.toCameraContractOptions(uri)
    
          launchContract({ cameraLauncher.launch(contractOptions) }, options)
        }
    

    but i check the permission on react-native code manually

    and it works like charm, i think there’s problem with permission mechanism on kotlin code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search