简体   繁体   English

在 Android 上使用 launchCameraAsync() 后,Expo-image-picker 使应用程序崩溃

[英]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.我正在使用 expo-image-picker 来允许用户选择和拍照。 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.我想我拥有所有必需的权限,而且问题似乎只发生在内存有限的旧版 Android 设备上。 Any ideas?有任何想法吗?

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.发生的事情是,当您启动相机时,它会作为一个新的Activity启动,将您的应用程序mainActivity在后台。

From Android P (9) and on, the OS can kill your background activity.从 Android P (9) 开始,操作系统可以终止您的后台活动。 There is even a website just about whose implementation is worse for devs: https://dontkillmyapp.com/甚至有一个网站的实施对开发人员来说更糟糕: https ://dontkillmyapp.com/


Possible solutions:可能的解决方案:

  1. Be prepared to crash.准备崩溃。 Save all states and navigation stack on local-storage, launchCamera.将所有状态和导航堆栈保存在本地存储、launchCamera 上。 The app will restart, you will restore all the screen/navigation/data/etc, and get photo using the ImagePicker.getPendingResultAsync .该应用程序将重新启动,您将恢复所有屏幕/导航/数据/等,并使用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.将 lib 更改为expo-camerareact-native-vision-camera ,因为它们在mainActivity上使用相机但需要无聊的重新实现,并且看起来不如制造商原生相机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 expo-image-picker android 没有选择按钮 - expo-image-picker android doesn't have a select button 如何将 expo-image-picker 与 android 11 范围存储一起使用? - how to use expo-image-picker with android 11 scoped storage? 使用 expo-image-picker 选择图像时,我的本机应用程序不断崩溃 - My react native app keeps crashing when selecting an image with expo-image-picker 任务 ':expo-image-picker:compileDebugKotlin' 执行失败 - Execution failed for task ':expo-image-picker:compileDebugKotlin' expo-image-manipulator 不会从 expo-image-picker 中获取 uri - expo-image-manipulator won't take uri from expo-image-picker 是什么导致 Expo 的 ImagePicker.launchCameraAsync() 在某些 android 设备上使应用程序崩溃? - What's causing Expo's ImagePicker.launchCameraAsync() to crash the app on some android devices? Android 使用相机意图捕获图像后应用程序崩溃 - Android App crashes after capturing image using camera intent 图像选择器插件在Android上崩溃 - Image picker plugin crashes on Android 移动图像按钮后,应用程序崩溃! 安卓 - App Crashes after moving an image button! android Expo MediaLibrary.getAssetsAsync() 使 Android 上的应用程序崩溃 - Expo MediaLibrary.getAssetsAsync() crashes the app on android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM