Search code examples
androidreact-nativereact-native-image-picker

React native image picker causes application to restart without error


I am using react-native-image-picker 2.3.1, react-native 0.63.3 and react 16.3.1. When using the image picker to launch the camera, on some devices (Samsung devices, Android 10, both tablets and phones), after the user takes the picture, the react-native application will reinitialize itself without any error or proper warning. The behavior is not consistent: sometimes, the user can only take 1 photo and the application restarts, other times it happens on the 33th photo taken. The behavior could not be reproduced in emulator nor in different devices like One plus or Motorola. The system logcat log only shows few relevant details, the ones I could narrow done being:

  • 15533-15533/? E/com.sfsdriver: Unknown bits set in runtime_flags: 0x8000

  • E/InputDispatcher: channel 'd0f2b6f com.sfsdriver/com.sfsdriver.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed! (probably happening after the app is no longer active)

  • E/memtrack: Couldn't load memtrack module

There are also lines like this: 7165-13038/com.sfsdriver I/ReactNativeJNI: Memory warning (pressure level: TRIM_MEMORY_COMPLETE) received by JS VM, running a GC which happen both when the application succedes in returning to the application activity after taking a picture and when the application restarts.

I also tried with the following react-native libraries: expo-image-picker, react-native-image-picker:v3.0.0-vnext.3


Solution

  • Reverting with information regarding this issue. Full history of discussions can be followed here: https://github.com/react-native-image-picker/react-native-image-picker/issues/1502. The summary is as follows: the issue happens on devices where the used memory is close to maximum available memory and is happening with almost all react-native libraries that launch a new Activity for camera (expo-image-picker, react-native-crop-image-picker, etc.). When this happens the Android OS has the ability to kill background tasks to reclaim memory (Some proprietary versions of Android can kill background tasks more aggresively than others). When the user launches a new intent to open the camera, the main React native application goes in background and can be killed by Android OS (and re-created by Android OS when the user returns to the initial application). When the application is killed, all information is lost unless you have a mechanism to persist state when going in background. Usually, the Android OS will provide the last Activity result (in this case the image taken with the camera application) to the calling application which in this case is the React Activity. Unfortunately there is an issue with how React native handles this onActivity result when the React Context is destroyed and later re-created, issue that can be followed here: https://github.com/facebook/react-native/issues/30277.

    Until that issue is fixed, this problem will persist, mostly in the case of low-memory devices. For anyone not looking for fancy camera options, just the ability to take pictures or start video recording, you can use the React Native Camera library (https://github.com/react-native-camera/react-native-camera) to overcome this issue with activity being re-created by the Android system. The difference is that the RNCamera library does not launch a new Android activity (just embeds the camera wherever you specify it to do it) which means that your current React application will remain active, even when taking picture/recording video.