【问题标题】:React Native CameraRoll permission deniedReact Native CameraRoll 权限被拒绝
【发布时间】:2019-04-26 10:48:21
【问题描述】:

按下按钮后会执行以下代码:

takePicture = async function() {
    if (this.camera) {
      const options = { quality: 0.5, base64: true }
      const data = await this.camera.takePictureAsync(options)
      CameraRoll.saveToCameraRoll(data.uri)
    }
  }

调试器显示:

Possible Unhandled Promise Rejection
Error: Permission Denied ...

显然,CameraRoll 需要用户的许可才能执行此操作,但我已经将它们包含在我的 AndroidManifest.xml 文件中:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.gradualcamera">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <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" />

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>

</manifest>

整个组件的代码:

import React, {Component} from 'react';
import {StyleSheet, View} from 'react-native'
import { RNCamera } from 'react-native-camera'
import { CameraRoll } from 'react-native'
import ActionButton from 'react-native-action-button'
import Icon from 'react-native-vector-icons/Ionicons'

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },

  button: {
    height: 200,

    justifyContent: 'flex-end',
    alignItems: 'center'
  },

  actionButtonIcon: {
    fontSize: 20,
    height: 22,
    color: 'white',
  },

});


export default class Cam extends Component {
  constructor() {
    super()
    this.takePicture = this.takePicture.bind(this)
  }

  takePicture = async function() {
    if (this.camera) {
      const options = { quality: 0.5, base64: true }
      const data = await this.camera.takePictureAsync(options)
      CameraRoll.saveToCameraRoll(data.uri)
    }
  }

  render() {
    return (
      <View style={styles.container}>

        <RNCamera
          ref={ref => {this.camera = ref}}
          style={{
            flex: 1,
            width: '100%',
            position: 'relative'
          }}
        >
        </RNCamera>

        <ActionButton size={80} useNativeFeedback={false} buttonColor="rgba(231,76,60,1)">
          <ActionButton.Item useNativeFeedback={false} buttonColor='#9b59b6' title="Settings" onPress={this.props.switchScreen}>
            <Icon name="md-create" style={styles.actionButtonIcon} />
          </ActionButton.Item>
          <ActionButton.Item useNativeFeedback={false} buttonColor='#1abc9c' title="Start" onPress={this.takePicture}>
            <Icon name="md-done-all" style={styles.actionButtonIcon} />
          </ActionButton.Item>

        </ActionButton>

      </View>
    )
  }
}

我已经尝试删除应用程序并重新启动 Metro 服务器。

我从“设置”中手动启用了存储权限,现在可以使用了。但是,当我删除应用程序并再次重新安装时,不会向用户请求存储权限。我尝试使用 react-native-permissions 请求它,但它不起作用:


    componentDidMount() {
      _requestPermission = () => {
        Permissions.request('storage').then(response => {
          // Returns once the user has chosen to 'allow' or to 'not allow' access
          // Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
          console.log(response)
        })
      }
    }

在我将 react-native-permissions 方法从 onComponentMount 方法中移除并使用按钮触发它后,不知何故开始工作了。

【问题讨论】:

    标签: javascript android react-native react-native-camera


    【解决方案1】:

    只需添加这一行AndroidManifest.xml

    <application tag android:requestLegacyExternalStorage="true"
    

    【讨论】:

      【解决方案2】:

      看看这个组件,在使用相机之前检查是否启用了权限。

      https://github.com/yonahforst/react-native-permissions

      【讨论】:

      • 我检查了这个库的权限,它说它是未确定的,这是有道理的。但是,当我使用此库请求权限时,该窗口不会显示。另外,我很困惑 React Native Camera 似乎有一个开箱即用的权限请求。你能澄清一下吗?提前谢谢你。
      • 摄像头权限已启用,但未启用 CameraRoll 使用权限。
      • 可能发生这种情况是因为您第一次拒绝了权限。转到设置并转到应用程序,然后搜索该应用程序并输入她的权限。检查那里有什么
      • 我从设置中手动启用了存储权限,现在可以使用了。但是,当我删除应用程序并再次重新安装时,不会向用户请求存储权限。我尝试使用 react-native-permissions 请求它,但它不起作用(您可以在我更新的帖子中看到代码)。
      • 正在请求相机权限并且工作正常(因为我可以拍照并将它们发送到缓存中)。问题出在 CameraRoll(它将图片发送到 DCIM 文件夹)。我只需要解决 react-native-permissions 并希望请求存储权限,以便 CameraRoll 能够工作。
      猜你喜欢
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 2017-05-23
      • 2018-08-04
      • 2019-05-28
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      相关资源
      最近更新 更多