【问题标题】:React-native Access Device Camera? (Android Specifically)React-native 访问设备摄像头? (特别是安卓)
【发布时间】:2018-02-14 04:49:48
【问题描述】:

我正在尝试允许用户在 react-native 应用程序中上传视频,但我什至无法访问相机。

** 不使用世博会。我使用 react-native cli 并使用 react-native-init 生成我的项目**

** 使用 react-native 版本 0.53.0。安卓版本 5.1 **

** 更新:经过一些好的建议,我已将我的 android/app/build.gradle 文件中的 compileSdkVersions 和 targetSdkVersions 更改为 > 23,并且知道问题不在于我的权限。此外,已弃用的 RCTCamera 版本的 react-native-camera 工作正常,我能够捕获静止图像和视频并将它们保存到设备中。只有使用 RNCamera 的主版本,每次屏幕加载时仍然会崩溃应用程序:/

这不太理想,因为我不想在我的应用中包含已弃用的代码。所以当我在下面提到 react-native-camera 时,我指的是使用 RNCamera 而不是 RCTCamera 的当前版本。 **

我尝试使用 react-native-camera 包 (https://github.com/react-native-community/react-native-camera) 但这会导致我的应用程序每次都崩溃。另外,我想使用用户已经安装的相机应用,而不是构建自己的相机视图,而这正是 react-native-camera 所需要的。

环顾四周,我偶然发现了三种有希望的方法来解决这个问题:

1) 链接 - 据我从 StackOverflow 帖子 (React native send a message to specific whatsapp Number2) 中了解到,链接可用于打开用户在其设备上拥有的其他应用程序。我认为这也可以用来访问相机应用程序。但我还没有找到任何有关这方面的信息。如何检查用户是否拥有相机应用程序,然后链接到他们?理想情况下,用户手机上会出现一个弹出菜单,要求用户从可用的相机应用列表中进行选择。

2) 这篇来自 android 开发者文档的帖子 - https://developer.android.com/training/camera/videobasics.html 。这描述了如何准确地做我想做的事,但是我在制作用于我的组件的本机模块时遇到了麻烦。我对在 react-native 中构建桥梁有非常基本的知识,并且只有在阅读了几篇列出所有代码的文章后才能使一个简单的本机 Toast 模块工作。那么任何人都可以编写一个 VideoModule.java 文件来实现与 android 文档指定的相同功能吗?这对我来说似乎是最简单的解决方案,但我对 Java/android 的了解不足阻碍了我。

3) ReactNativeWebRTC - 我已经成功地将这个模块 (https://github.com/oney/react-native-webrtc) 包含在应用程序的不同屏幕上。但是当我使用这个模块在两个对等点之间流式传输视频时,它看不到如何使用它来上传视频。我查看了是否有类似于我用于应用程序网络版本的 MediaRecorder API 的东西,但我没有任何运气。但我知道,如果我可以从媒体流中获取二进制数据,那么我可以将其直接发送到我的服务器。那么,有没有办法直接将 react-native-webrtc 使用的 getUserMedia() 方法中的媒体流存储在缓冲区中,而无需像 web 上的 MediaRecorder?

在这里,任何解决方案都会非常有帮助。而且由于我目前只有一部 Android 手机可供测试,因此我不需要有关如何使其与 iOS 一起使用的信息。只是安卓的解决方案。非常感谢。

这是我的 AndroidManifest.xml 权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我的 android/app/build.gradle 文件:

apply plugin: "com.android.application"

import com.android.build.OutputFile

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load 
the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation
 *   entryFile: "index.android.js",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
  *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

 project.ext.react = [
    entryFile: "index.js"
 ]

 apply from: "../../node_modules/react-native/react.gradle"

 /**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
 def enableSeparateBuildPerCPUArchitecture = false

  /**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
 def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion 26
    buildToolsVersion "25.0.2"

defaultConfig {
    applicationId "com.slimnative"
    minSdkVersion 16
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    ndk {
        abiFilters "armeabi-v7a", "x86"
    }
}
splits {
    abi {
        reset()
        enable enableSeparateBuildPerCPUArchitecture
        universalApk false  // If true, also generate a universal APK
        include "armeabi-v7a", "x86"
    }
}
buildTypes {
    release {
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        // For each separate APK per architecture, set a unique version code as described here:
        // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
        def versionCodes = ["armeabi-v7a":1, "x86":2]
        def abi = output.getFilter(OutputFile.ABI)
        if (abi != null) {  // null for the universal-debug, universal-release variants
            output.versionCodeOverride =
                    versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
        }
    }
    }
   }

dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+"  // From node_modules
compile project(':WebRTCModule')
compile project(':react-native-svg')
compile (project(':react-native-camera')) {
    // exclude group: "com.google.android.gms"
    exclude group: "com.android.support"
}
// compile ("com.google.android.gms:play-services-vision:10.2.0") {
//     force = true;
// }
compile ('com.android.support:exifinterface:26.0.1') {
    force = true;
}
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}

还有我的 android/build.gradle:

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    mavenLocal()
    jcenter()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
    maven { url "https://jitpack.io" }
    maven {
        url "https://maven.google.com"
    }
   }
   }

【问题讨论】:

  • 先告诉我你用 expo 运行应用程序还是什么?并且您是否要求在 android 中获得运行时摄像头权限
  • @SagarChavada 我没有参加世博会。我使用 react-native cli 并使用 react-native init 来创建我的应用程序。我尝试直接从 react-native-camera 复制/粘贴示例(github.com/react-native-community/react-native-camera/tree/…),但是当我点击上传页面时应用程序崩溃了。即使我添加了所有权限,也没有请求在任何地方访问我的相机。奇怪的是,在使用 react-native-webrtc 模块时,我从来没有得到提示允许我的相机,尽管它工作得很好。那么也许这个模块是短路权限?
  • @SagarChavada 也已经尝试过使用 PermissionsAndroid。我仍然没有收到任何权限请求。相反,我得到了一个空白的黑屏
  • ok.. 只需发布您的 .gradle 代码,并始终提及有问题的 react-native 版本...

标签: android react-native android-camera webrtc react-native-native-module


【解决方案1】:

嘿@mraaron 我刚刚制作了一个 React Native 应用程序,我必须在其中制作和上传视频,基本上你可以使用 2 种方法:

1) React 原生图像选择器 https://github.com/react-community/react-native-image-picker哪个 打开本机摄像头录制视频,作为回应 u 路径和其他信息。该模块具有以下两种功能 定义选项时的图像和视频,您可以指定 mediaType: iOS 上的“照片”、“视频”或“混合”,Android 上的“照片”或“视频”

.

2) 反应原生相机 https://github.com/react-native-community/react-native-camera 在这 您可以自定义相机窗口,因为它不打开本机 相机应用

Note:- I have implemented both the packages and both are working absolutely fine in android as well as ios, if u need any help u can ping me up.

【讨论】:

  • 感谢您的信息!我将在今天晚些时候试用 react-native-image-picker!虽然从我现在的快速浏览来看,这似乎是解决我的问题的更好方法
  • 经过测试,我认为react-native-image-picker是这里的最佳选择。你在这里为我省去了很多麻烦
  • PSA。我发现 react-native-image-picker 在 Android 上是有问题的。可能是我的情况独有的东西,但它可以完美运行,直到您第二次尝试更改照片,此时它崩溃并重新启动应用程序。该请求仍然通过并更新照片。 ¯_(ツ)_/¯ "react-native-image-picker": "^0.26.10"; "react-native": "0.51.0",
  • 感谢您的信息,我想知道有没有办法在 Android 上同时拥有视频和图像?
【解决方案2】:

这是我昨天刚做的演示......如果有帮助:

import React from 'react';
import { View, Text, Alert } from 'react-native';
import { BarCodeScanner, Permissions } from 'expo';

    class CameraForm extends React.Component {

      state = {
        hasCameraPermission: null
      };

      componentDidMount() {
        this.permissionCheck();
      }

      permissionCheck = async () => {
        const { status } = await Permissions.askAsync(Permissions.CAMERA);
        this.setState({
          hasCameraPermission: status === 'granted'
        });
      };

      handleBarCodeScanRead = data => {
          Alert.alert(
            'Scan successful!',
            JSON.stringify(data)
          );
      };

      render() {
        return (
          <View style={styles.container}>
            <Text>Scan your wallet code</Text>
            { this.state.hasCameraPermission === null ?
                  <Text>Requesting for camera permission</Text> :
                  this.state.hasCameraPermission === false ?
                  <Text>Camera permission is not granted</Text> :
                  <BarCodeScanner
                    onBarCodeRead={this.handleBarCodeScanRead}
                    style={{ height: 400, width: 400, marginTop: 20 }}
                  />
            }
          </View>
        );
      }
    }

    const styles = {
      container: {
        flex: 1,
        alignSelf: 'stretch',
        alignItems: 'center',
        justifyContent: 'center',
        backgroundColor: 'white'
      }
    };

    export default CameraForm

;

【讨论】:

  • 我会尝试在我的应用中使用您的权限模型。感谢分享这个
【解决方案3】:

关于应用程序每次崩溃,您是否仔细检查过您是否在 androidmanifest.xml/info.plist 中提到了应用程序所需的所有权限?

另外,您希望将外部应用程序用于相机视图的具体原因是什么? 因为我使用过 react-native-camera 并且可以无缝运行。

【讨论】:

  • 是的,我已经在 androidmanifest.xml 文件中检查了我的所有权限。我会将我的文件添加到我的问题中,以便您查看。而且我想使用外接摄像头,以便用户有更大的舒适感,因为他们可能一直习惯于使用该应用程序。但它不是必须的,它是外部的
  • 如果您可以上传 android manifest 那就太好了,我建议您使用 react-native-camera 视图,因为这样您就可以控制您认为应用程序的相机视图应该是什么样子,话虽如此,我的意思是说,例如,您正在设计此应用程序以仅从相机视图中读取条形码,然后使用您的自定义视图,您可以呈现一个简单的视图,避免使用 faceDetection、从画廊中挑选图像等。
  • 我刚刚添加了我的清单文件。谢谢推荐,我会考虑的。希望我只是在某个地方有一些愚蠢的错字,我可以使用 react-native-camera
  • 我希望你没有在“前置”模式下使用摄像头视图,因为我没有看到使用前置摄像头的权限
  • 不用担心..没有前置摄像头的权限...只需要访问摄像头..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
相关资源
最近更新 更多