一、起因
app调用了 'react-native-image-crop-picker' ,发现在ios下调起摄像头/相册功能正常,但是在安卓下无法调起摄像头。
react-native 版本:0.60.5
原代码引入:
import ImagePicker from 'react-native-image-crop-picker';
// 调起摄像头
ImagePicker.openCamera({
width: 300,
height: 300,
cropping: true
}).then(image => {
if (image && image.path) {
this.uploadHeadIcon(image.path); //上传头像的函数
};
}).catch(e => console.log(e));
// 调起相册
ImagePicker.openPicker({
width: 300,
height: 300,
cropping: true
}).then(image => {
if (image && image.path) {
this.uploadHeadIcon(image.path)
}
}).catch(e => console.log(e));
ios直接像上面这样引入就没有问题(当然yarn和pod install 是必须做的),但安卓会出现摄像头无法调起的情况
二、解决方法
1. 检查android/build.gradle 文件,是否有下面代码。
allprojects {
repositories {
mavenLocal()
jcenter()
maven { url "$rootDir/../node_modules/react-native/android" }
// ADD THIS
maven { url 'https://maven.google.com' }
// ADD THIS
maven { url "https://jitpack.io" }
}
}
2. 增加useSupportLibrary权限,检查android/app/build.gradle
android {
...
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
...
}
...
}
3. 检查 Android SDK >= 26 (android/app/build.gradle)
4. 如果要调起摄像头,检查 app\src\main\AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
5. 如果要用前置摄像头
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
6. 到这里,上面的配置都正确,就可以调起安卓摄像头了。其实上面步骤npm包都有写,但是导入的时候不会仔细去检查每一步,所以写篇文章记录一下。
参考:https://www.npmjs.com/package/react-native-image-crop-picker