React Native 当选择50MP相机时,使用Expo Image Picker拍照会在Android上崩溃

os8fio9y  于 2023-10-23  发布在  React
关注(0)|答案(1)|浏览(120)

我的应用程序是崩溃时,使用Android设备上的图像拾取器与相机配置设置为3:4 50MP拍照.有时,当我在拍照后按下OK时,应用程序就会重新启动。
它发生在我的一个生产应用程序中,所以我认为这可能与我的代码有关,但我创建了一个新的世博会应用程序,只有一个按钮可以打开相机,而且它也会在点击OK按钮时崩溃
不是每次都这样,但我找不到规律。控制台也不显示日志或消息
我尝试了很多不同的东西:拍摄具有许多颜色和信息的照片,拍摄黑色屏幕,白色屏幕等的照片仍然随机发生,但只有当设置为50 MP时
我的App.js和package.json如下
package.json

{
  "name": "camera-test",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web"
  },
  "dependencies": {
    "expo": "~49.0.10",
    "expo-status-bar": "~1.6.0",
    "react": "18.2.0",
    "react-native": "0.72.4",
    "expo-image-picker": "~14.3.2",
    "expo-updates": "~0.18.12"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0"
  },
  "private": true
}

App.js

import * as ImagePicker from 'expo-image-picker';
import React, { useState } from 'react';
import { Button, View } from 'react-native';

export default function App() {
  const [image, setImage] = useState(null);

  const takePicture = async () => {
    const c = await ImagePicker.requestCameraPermissionsAsync();

    if (c.status === "granted") {
      let result = await ImagePicker.launchCameraAsync({
        aspect: [4, 3],
        quality: 0.1,
      });
    }
  };

  return (
    <View style={ { flex: 1, alignItems: 'center', justifyContent: 'center' } }>
      <Button title="Take a picture " onPress={ takePicture } />
    </View>
  );
}

Android Studio日志

FATAL EXCEPTION: main    Process: com.my.package, PID: 27880 java.lang.RuntimeException: Unable to resume activity {com.my.package/com.my.package.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=219778226, result=-1, data=null} to activity {com.my.package/com.my.package.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.react.ReactDelegate.onActivityResult(int, int, android.content.Intent, boolean)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5378)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5444)
 at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
 at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
 at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
 at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574)
 at android.os.Handler.dispatchMessage(Handler.java:106)
 at android.os.Looper.loopOnce(Looper.java:226)
 at android.os.Looper.loop(Looper.java:313)
 at android.app.ActivityThread.main(ActivityThread.java:8757)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
 Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=219778226, result=-1, data=null} to activity {com.my.package/com.my.package.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.react.ReactDelegate.onActivityResult(int, int, android.content.Intent, boolean)' on a null object reference
 at android.app.ActivityThread.deliverResults(ActivityThread.java:5994)
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5348)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5444) 
 at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54) 
 at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
 at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
 at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574) 
 at android.os.Handler.dispatchMessage(Handler.java:106) 
 at android.os.Looper.loopOnce(Looper.java:226) 
 at android.os.Looper.loop(Looper.java:313) 
 at android.app.ActivityThread.main(ActivityThread.java:8757) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.react.ReactDelegate.onActivityResult(int, int, android.content.Intent, boolean)' on a null object reference
 at com.facebook.react.ReactActivityDelegate.onActivityResult(ReactActivityDelegate.java:133)
 at com.facebook.react.ReactActivity.onActivityResult(ReactActivity.java:70)
 at android.app.Activity.dispatchActivityResult(Activity.java:8951)
 at android.app.ActivityThread.deliverResults(ActivityThread.java:5987)
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5348) 
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5444) 
 at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54) 
 at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
 at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
 at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574) 
 at android.os.Handler.dispatchMessage(Handler.java:106) 
 at android.os.Looper.loopOnce(Looper.java:226) 
 at android.os.Looper.loop(Looper.java:313) 
 at android.app.ActivityThread.main(ActivityThread.java:8757) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 
2023-09-29 14:24:21.064 27880-27880 DevLauncher  com.my.package  E  DevLauncher tries to handle uncaught exception.
 java.lang.RuntimeException: Unable to resume activity {com.my.package/com.my.package.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=219778226, result=-1, data=null} to activity {com.my.package/com.my.package.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.react.ReactDelegate.onActivityResult(int, int, android.content.Intent, boolean)' on a null object reference
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5378)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5444)
 at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
 at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
 at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
 at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574)
 at android.os.Handler.dispatchMessage(Handler.java:106)
 at android.os.Looper.loopOnce(Looper.java:226)
 at android.os.Looper.loop(Looper.java:313)
 at android.app.ActivityThread.main(ActivityThread.java:8757)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
 Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=219778226, result=-1, data=null} to activity {com.my.package/com.my.package.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.react.ReactDelegate.onActivityResult(int, int, android.content.Intent, boolean)' on a null object reference
 at android.app.ActivityThread.deliverResults(ActivityThread.java:5994)
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5348)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5444) 
 at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54) 
 at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
 at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
 at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574) 
 at android.os.Handler.dispatchMessage(Handler.java:106) 
 at android.os.Looper.loopOnce(Looper.java:226) 
 at android.os.Looper.loop(Looper.java:313) 
 at android.app.ActivityThread.main(ActivityThread.java:8757) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.react.ReactDelegate.onActivityResult(int, int, android.content.Intent, boolean)' on a null object reference
 at com.facebook.react.ReactActivityDelegate.onActivityResult(ReactActivityDelegate.java:133)
 at com.facebook.react.ReactActivity.onActivityResult(ReactActivity.java:70)
 at android.app.Activity.dispatchActivityResult(Activity.java:8951)
 at android.app.ActivityThread.deliverResults(ActivityThread.java:5987)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5348) 
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5444) 
 at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54) 
 at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
 at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
 at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574) 
 at android.os.Handler.dispatchMessage(Handler.java:106) 
 at android.os.Looper.loopOnce(Looper.java:226) 
 at android.os.Looper.loop(Looper.java:313) 
 at android.app.ActivityThread.main(ActivityThread.java:8757) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 
6ioyuze2

6ioyuze21#

您的代码中有一个错误:

if (c.status === "granted") {
  let result = await ImagePicker.launchCameraAsync({
    aspect: [4, 3],
    quality: 0.1,
  });
}
console.log(result);

console.log(result);行中,变量result未定义。你要像这样把它向上移动:

if (c.status === "granted") {
  let result = await ImagePicker.launchCameraAsync({
    aspect: [4, 3],
    quality: 0.1,
  });
  console.log(result);
}

相关问题