androidreact-nativeexpoimagepicker

Expo-image-picker crashes app after using launchCameraAsync() on Android


I'm using expo-image-picker to allow the user to pick and take pictures. Choosing an image from the library works as expected but when using the camera, after taking the picture the app crashes. Here is my code:

const take = async () => {
    let result = await ImagePicker.launchCameraAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.Images,
      quality: 1,
      
    });

    console.log(result.uri);
    if (!result.cancelled) {
      setImageUri(result.uri);
    }
  };

I think I have all the required permissions and the problem seems to occur only on older android devices with limited memory. Any ideas?


Solution

  • There is no good and easy solution.

    What is happening is, when you launch the camera, it launches as a new Activity, leaving your app mainActivity in background.

    From Android P (9) and on, the OS can kill your background activity. There is even a website just about whose implementation is worse for devs: https://dontkillmyapp.com/


    Possible solutions:

    1. Be prepared to crash. Save all states and navigation stack on local-storage, launchCamera. The app will restart, you will restore all the screen/navigation/data/etc, and get photo using the ImagePicker.getPendingResultAsync.
    2. Change lib to expo-camera or react-native-vision-camera as they use the camera on mainActivity but needs boring reimplementation, and don' t look as good as the manufacturer native cameras.