我最近退出了我的本地反应项目,并且相机功能不起作用。该代码允许用户上传图像。它可以与expo一起使用,但是在弹出并在Android上运行后,它停止了工作。这是摄像机上传的代码:
import * as Permissions from 'expo-permissions';
import * as ImagePicker from 'expo-image-picker';
_checkPermissions = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({
camera: status
});
// take permission for Gallery, aka CameraRoll
const { statusRoll } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
this.setState({
cameraRoll: statusRoll
});
};
findNewImage = async () => {
this._checkPermissions();
let result = await ImagePicker.launchCameraAsync({
mediaTypes: "Images",
allowsEditing: true,
quality: 1
});
console.log("image picked is = result = ", result);
if (!result.cancelled) {
console.log("upload image");
this.setState({
imageSelected: true,
imageId: this.uniqueId(),
uri: result.uri
});
} else {
console.log("cancel");
this.setState({
imageSelected: false
});
}
};
但是,出现此错误:
W/ReactNativeJS: Possible Unhandled Promise Rejection (id: 3):
TypeError: undefined is not an object (evaluating '_ExpoPermissions.default.askAsync')
askAsync$@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:172905:109
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23241:32
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23142:30
http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23172:19
tryCallTwo@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24367:9
doResolve@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24531:25
Promise@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24390:14
callInvokeWithMethodAndArg@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23171:29
enqueue@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23176:157
async@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23192:69
askAsync@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:172896:38
_callee$@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141098:71
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23241:32
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23142:30
http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23172:19
tryCallTwo@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24367:9
doResolve@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24531:25
Promise@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24390:14
callInvokeWithMethodAndArg@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23171:29
enqueue@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23176:157
async@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23192:69
_callee@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141093:42
_callee2$@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141141:40
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23241:32
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23142:30
http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23172:19
tryCallTwo@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24367:9
doResolve@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24531:25
Promise@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24390:14
callInvokeWithMethodAndArg@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23171:29
enqueue@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23176:157
async@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23192:69
_callee2@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141137:42
onPress@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141457:39
touchableHandlePress@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:51687:47
touchableHandlePress@[native code]
_performSideEffectsForTransition@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:50367:36
_performSideEffectsForTransition@[native code]
_rec
W/ReactNativeJS: Possible Unhandled Promise Rejection (id: 4):
TypeError: undefined is not an object (evaluating '_ExponentImagePicker.default.launchCameraAsync')
launchCameraAsync$@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:173085:45
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23241:32
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23142:30
http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23172:19
tryCallTwo@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24367:9
doResolve@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24531:25
Promise@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24390:14
callInvokeWithMethodAndArg@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23171:29
enqueue@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23176:157
async@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23192:69
launchCameraAsync@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:173079:38
_callee2$@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141144:80
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23241:32
tryCatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23066:23
invoke@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23142:30
http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23172:19
tryCallTwo@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24367:9
doResolve@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24531:25
Promise@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:24390:14
callInvokeWithMethodAndArg@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23171:29
enqueue@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23176:157
async@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:23192:69
_callee2@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141137:42
onPress@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141457:39
touchableHandlePress@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:51687:47
touchableHandlePress@[native code]
_performSideEffectsForTransition@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:50367:36
_performSideEffectsForTransition@[native code]
_receiveSignal@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:50293:46
_receiveSignal@[native code]
touchableHandleResponderRelease@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:50172:26
touchableHandleResponderRelease@[native code]
invokeGuardedCallbackImpl@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:8533:21
invokeGuardedCallback@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:8624:42
invokeGuardedCallbackAndCatchFirstError@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:8628:36
executeDispatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:8822:48
executeDispatchesInOrder@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:8842:26
executeDispatchesAndRelease@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:8937:35
forEach@[native code]
forEachAccumulated@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:8927:22
runEventsInBatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:9031:27
runExtrac
我的应用程序中有一个上传按钮,当我单击它时,此错误消息出现在Android Studio控制台中。与Expo的配合正常。
这是我的package.json:
{
"scripts": {
"start": "react-native start",
"android": "react-native run-android",
"ios": "react-native run-ios",
"web": "expo start --web"
},
"dependencies": {
"@expo/samples": "~3.0.0",
"@expo/vector-icons": "^10.0.2",
"@unimodules/core": "^2.0.1",
"@unimodules/react-native-adapter": "^2.0.1",
"expo-asset": "^5.0.0",
"expo-constants": "^5.0.0",
"expo-file-system": "^5.0.1",
"expo-font": "^5.0.0",
"expo-image-picker": "^5.0.2",
"expo-permissions": "^5.0.1",
"expo-web-browser": "^5.0.0",
"firebase": "^6.1.1",
"react": "16.8.3",
"react-dom": "^16.8.6",
"react-native": "0.59.8",
"react-native-elements": "^1.1.0",
"react-native-gesture-handler": "^1.3.0",
"react-native-paper": "^2.16.0",
"react-native-permissions": "^1.1.1",
"react-native-vector-icons": "^6.5.0",
"react-native-web": "^0.11.4",
"react-navigation": "^3.11.0",
"react-navigation-material-bottom-tabs": "^1.0.0",
"remove-node-modules": "^1.7.4"
},
"devDependencies": {
"babel-preset-expo": "^5.1.0"
},
"private": true
}
答案 0 :(得分:0)
如果要带出 React-native project
,而不是 Expo
,
您应该使用React-native-unimodules
而不是自己安装模块。
npm install react-native-unimodules
该库包含核心Unimodule基础结构以及其他Unimodule通常依赖的Unimodule和接口的集合。您需要先安装此软件,然后才能使用Foundation中的库,例如expo-camera,expo-media-library等。
配置Android
向AndroidManifest.xml添加权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
您可能不必直接使用此软件包提供的任何代码,只能由您安装的其他Unimodules使用。
但是您很可能希望使用诸如FileSystem或Permissions之类的方法,并且可以这样导入以下模块:
import {
Asset,
Constants,
FileSystem,
Permissions,
} from 'react-native-unimodules';
如果您想了解更多about this,
答案 1 :(得分:0)
我在使用react-native-contacts时也遇到了相同的错误。 尝试在.then()之后添加.catch()块,它解决了我的问题。
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
{
'title': 'Contacts',
'message': 'This app would like to view your contacts.'
}
).then(() => {
con.getAll((err, contacts) => {
if (err === 'denied'){
// error
} else {
// contacts returned in Array
console.log(contacts);
}
})
})
.catch((err)=> {
console.log(err);
})
答案 2 :(得分:0)
总是尝试为所有等待的事件添加一个捕获块。
例如
await Permissions.askAsync(Permissions.CAMERA).catch(err=>{});
也适用于其他地方。