概述
避免再次踩坑,特地记录一下这段时间用RN成功了的一个使用流程。
第一步
**要求**
1.JDK> = 1.7(如果你在1.6上运行,你会得到一个错误“_cameras = new HashMap <>();”)
2.使用iOS 10及更高版本,您需要将“隐私 - 相机使用说明”键添加到项目的Info.plist中。这应该在'your_project / ios / your_project / Info.plist'中找到。添加以下代码:
<key>NSCameraUsageDescription</key>
<string>Your message to user when the camera is accessed for the first time</string>
<!-- Include this only if you are planning to use the camera roll -->
<key>NSPhotoLibraryUsageDescription</key>
<string>Your message to user when the photo library is accessed for the first time</string>
<!-- Include this only if you are planning to use the microphone for video recording -->
<key>NSMicrophoneUsageDescription</key>
<string>Your message to user when the microphone is accessed for the first time</string>
3.在Android上,你需要buildToolsVersion的25.0.2+。这应该由Android Studio的SDK Manager轻松自动下载。
4.在iOS 11及更高版本中,您需要向NSPhotoLibraryAddUsageDescriptionInfo.plist 添加密钥。通过此键,您可以描述应用程序寻求对用户照片库的只写访问权限的原因。Info.plist可以在'your_project / ios / your_project / Info.plist'中找到。添加以下代码:
<!-- Include this only if you are planning to use the camera roll -->
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Your message to user when the photo library is accessed for the first time</string>
**主要使用react-native自动安装**
1.npm install react-native-camera --save //第一步npm 插件名
2.react-native link react-native-camera //link一些依赖,在settings.gradle文件中成功了即可看到
include ':react-native-camera'
project(':react-native-camera').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-camera/android')
3.Open up android/app/src/main/java/[…]/MainApplication.java
打开 android/app/src/main/java/[…]/MainApplication.java
Add import org.reactnative.camera.RNCameraPackage; to the imports at the top of the file
添加import org.reactnative.camera.RNCameraPackage;到文件顶部的导入
Add new RNCameraPackage() to the list returned by the getPackages() method. Add a comma to the previous item if there’s already something there.
添加new RNCameraPackage()到getPackages()方法返回的列表中。如果已经存在某些内容,请在前一项中添加逗号。
4.在依赖项块中插入以下行android/app/build.gradle:
compile (project(‘:react-native-camera’)) {
exclude group: “com.google.android.gms”
compile ‘com.android.support:exifinterface:25.+’
compile (‘com.google.android.gms:play-services-vision:12.0.1’) {
force = true
}
}
5.声明Android Manifest中的权限(video recording功能需要)
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6.添加到android / build.gradle
allprojects {
repositories {
maven {url “ https://jitpack.io ” }
maven {url “ https://maven.google.com ” }
}
}
当前的Android库默认为Google SDK和库的以下值
def DEFAULT_COMPILE_SDK_VERSION = 26
def DEFAULT_BUILD_TOOLS_VERSION = "26.0.2"
def DEFAULT_TARGET_SDK_VERSION = 26
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "12.0.1"
def DEFAULT_SUPPORT_LIBRARY_VERSION = "27.1.0"
您可以通过添加项目范围的gradle配置属性来覆盖此设置,以供ReactNative项目中的所有模块使用,方法是将以下内容添加到android/build.gradle 文件中
如果遇到什么问题,可以https://github.com/react-native-community/react-native-camera#android查看更多详情。