我弹出了我的react-native项目,并在android studio中对其进行了测试。但是,我不断收到错误消息:
ReferenceError:找不到变量:权限 ReferenceError:找不到变量:ImagePicker
我有一个上传按钮,可以让用户用自己的相机拍照并上传图片。
这是上传功能的本机代码:
import React from "react";
import { TextInput, ActivityIndicator, Image, TouchableOpacity, View, Text, StyleSheet } from "react-native";
import { f, auth, database, storage } from "../config/config.js";
import UserAuth from "../components/auth.js";
_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
});
}
};
这是android studio中的错误消息:
W/ReactNativeJS: Possible Unhandled Promise Rejection (id: 9):
ReferenceError: Can't find variable: Permissions
_callee$@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141092:62
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:141087:42
_callee2$@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141135: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:141131:42
onPress@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141451: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
runExtractedEventsInBatch@http://10.0.2.2:8081/index.delta?platform=android&dev=
W/ReactNativeJS: Possible Unhandled Promise Rejection (id: 10):
ReferenceError: Can't find variable: ImagePicker
_callee2$@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141138:62
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:141131:42
onPress@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:141451: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
runExtractedEventsInBatch@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:9038:25
http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:10005:36
batchedUpdates$1@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:21211:20
batchedUpdates@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:9947:37
_receiveRootNodeIDEvent@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:10004:23
receiveTouches@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:10034:34
__callFunction@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:2575:49
http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:2332:31
__guard@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:2529:15
callFunctionReturnFlushedQueue@http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false:2331:21
callFunctionReturnFlushedQueue@[native code]
答案 0 :(得分:1)
页面上没有导入权限的模块。为该页面添加权限模块。 React-native具有用于默认特权的模块。
有关权限,请参见here
示例
import {PermissionsAndroid} from 'react-native';
async function requestCameraPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: 'Cool Photo App Camera Permission',
message:
'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the camera');
} else {
console.log('Camera permission denied');
}
} catch (err) {
console.warn(err);
}
}