问题
I recently ejected my react-native project and the camera feature is not working. The code allows users to upload images. It works with expo but after eject and running with Android, it stopped working. This is code for the camera upload:
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
});
}
};
However, I get this error:
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
There is an upload button in my app and when I click it, this error message comes up in Android Studio console. It was working fine with Expo.
Here is my 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
}
回答1:
If you're bringing out a React-native project
, not an Expo
,
You should use React-native-unimodules
instead of installing modules yourself.
npm install react-native-unimodules
This library contains the core Unimodule infrastructure and a collection of Unimodules and interfaces that are commonly depended on by other Unimodules. You will need to install this before using libraries from Foundation, like expo-camera, expo-media-library, and others.
Configure Android
- At the top of android/settings.gradle, add apply from: '../node_modules/react-native-unimodules/gradle.groovy' and then on the next line add includeUnimodulesProjects().
- Add apply from: '../../node_modules/react-native-unimodules/gradle.groovy' anywhere in android/app/build.gradle and then addUnimodulesDependencies() inside dependencies {} block.
- If you need to customize the path to node_modules, for example because you are using yarn workspaces, then you can pass in a param modulesPaths for both of these functions: includeUnimodulesProjects([modulesPaths: ['./path/to/node_modules']]), addUnimodulesDependencies([modulesPaths: ['./path/to/node_modules']])
- If you need to exclude some of the unimodules that you are not using but they got installed by your other dependencies (like expo), then you can pass in exclude param for this. For example, if you want to exclude expo-face-detector, you may want to use this: addUnimodulesDependencies([exclude: ['expo-face-detector']])
- You can also customize the configuration of the unimodules dependencies (the default is implementation, if you're using Gradle older than 3.0, you will need to set configuration: "compile" in addUnimodulesDependencies, like: addUnimodulesDependencies([configuration: "compile"]))
- We recommend using Java 1.8, you can set this in android/app/build.gradle like this.
- Update minSdkVersion in android/build.gradle to 21.
- Update your MainApplication.java to according to this diff.
Add permissions to 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" />
It's possible that you will not have to use any of the code provided by this package directly, it may be used only by other Unimodules that you install.
But it's likely that you will want to use something like FileSystem or Permissions, and to do that you can import the following modules like so:
import {
Asset,
Constants,
FileSystem,
Permissions,
} from 'react-native-unimodules';
If you want to know more about this,
来源:https://stackoverflow.com/questions/56691470/w-reactnativejs-possible-unhandled-promise-rejection-id-3-and-4