0

我正在尝试使用 expo 图像选择器上传图像,但是当单击按钮并选择图像时,它会给出未处理的承诺拒绝 _expo.default.launchImageLibraryAsync 错误

已在 app json 中添加相机存储权限

import React from 'react';
import { StyleSheet, Text, TouchableOpacity, View, Image } from 'react-native';
import Expo from 'expo';
import ImagePicker from 'expo';
import * as Permissions from 'expo-permissions';

export default class App extends React.Component {
  state = {
    image: null,
  };

  selectPicture = async () => {
    await Permissions.askAsync(Permissions.CAMERA_ROLL);
    const { cancelled, uri } = await ImagePicker.launchImageLibraryAsync({
      aspect: 1,
      allowsEditing: true,
    });
    if (!cancelled) this.setState({ image: uri });
  };

  takePicture = async () => {
    await Permissions.askAsync(Permissions.CAMERA);
    const { cancelled, uri } = await ImagePicker.launchCameraAsync({
      allowsEditing: false,
    });
    this.setState({ image: uri });
  };

  render() {
    return (
      <View style={styles.container}>
        <Image style={styles.image} source={{ uri: this.state.image }} />
        <View style={styles.row}>
          <Button onPress={this.selectPicture}>Gallery</Button>
          <Button onPress={this.takePicture}>Camera</Button>
        </View>
      </View>
    );
  }
}

const Button = ({ onPress, children }) => (
  <TouchableOpacity style={styles.button} onPress={onPress}>
    <Text style={styles.text}>{children}</Text>
  </TouchableOpacity>
);

想上传图片

4

1 回答 1

0

您必须征得用户的适当许可才能访问相机。您必须permissions API从 Expo 导入,然后根据需要请求相机许可。添加权限App.json将不起作用,因为它不会要求用户允许使用相机。您必须添加 Permission API,然后向用户询问相机权限并根据权限渲染相机。

于 2019-08-16T05:12:47.057 回答