关于ImagePicker在android调用不起摄像头的解决办法

一、起因

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值