Search code examples
androidiosreact-nativeexponative

Camera capture stopped working after upgrading from Expo SDK 50 -> 51


So I just upgraded from SDK 50 -> 51 which made my app crash all the time I go to component with a Camera, now i fixed it, my current "take picture" method does not work. Do you guys know how to fix this?

const captureImage = async () => {
        if (camRef) {
            try {
                const imgData = await camRef.current.takePictureAsync({
                    base64: false,
                    imageType: ImageType.png,
                });

                setImage(imgData);
            } catch (err) {
                console.log(err);
            }
        }
    }

I tried using other methods but doesnt work


Solution

  • This example worked for me:

    async function takePicture() {
        if (cameraRef.current) {
          cameraRef.current.takePictureAsync({
            base64: false,
            imageType:ImageType.png,
            onPictureSaved: async (picture) => {
              console.log('Picture saved:', picture.uri)
              setImage(picture.uri as string);
              const asset = await MediaLibrary.createAssetAsync(picture.uri);
            }
          });
    
        }
      }
    
            <CameraView ref={cameraRef} style={styles.camera} facing={type as CameraType}>
              <View style={styles.buttonContainer}>
                <TouchableOpacity style={styles.button} onPress={toggleCameraType}>
                  <Text style={styles.text}>Flip Camera {selectedRatio}</Text>
                </TouchableOpacity>
                <TouchableOpacity style={styles.button} onPress={takePicture}>
                  <Text style={styles.text}>Take Picture</Text>
                </TouchableOpacity>
                {image && <Image source={{ uri: image }} style={{
                  flex: 1, width: 100, height: 100, alignSelf: 'flex-end',
                  alignItems: 'center', justifyContent: 'center', resizeMode: 'contain'
                }} />}
              </View>
            </CameraView>