【问题标题】:Expo Imagepicker asks for permissions but nothing else happensExpo Imagepicker 请求权限,但没有其他反应
【发布时间】:2019-08-05 14:46:35
【问题描述】:

我正在使用 expo 构建一个 react-native 应用程序。我正在尝试实现世博会 imagePicker,以便用户可以从他们的画廊或相机胶卷中选择图像并在应用程序上使用它,但是在 ImagePicker ahs 请求权限并且我允许它们之后,没有其他任何事情发生。你的用户不会被带到他们的画廊或 cameraRoll。目前没有错误,但 imagePicker 无法正常工作。 我需要对我的代码进行哪些更改?

ImageSelector.js 文件

import * as React from 'react';
import { Button, Image, View } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import * as Permissions from 'expo-permissions';
import * as Constants from 'expo-Constants';

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

  render() {
    let { image } = this.state;

    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Button
          title="Pick an image from camera roll"
          onPress={this._pickImage}
        />
        {image &&
          <Image source={{ uri: image }} style={{ width: 200, height: 200 }} />}
      </View>
    );
  }

  componentDidMount() {
    this.getPermissionAsync();
  }

  getPermissionAsync = async () => {
    if (Constants.platform.android) {
      const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
      if (status !== 'granted') {
        alert('Sorry, we need camera roll permissions to make this work!');
      }
    }
  }

  _pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [4, 3],
    });

    console.log(result);

    if (!result.cancelled) {
      this.setState({ image: result.uri });
    }
  };
}

【问题讨论】:

  • 您拥有所有权限吗?正如文档所说,“您还需要在 iOS 上添加 UsageDescription 并在 Android 上添加一些权限,请参阅安装文档。”
  • @SujilMaharjan 我还没有 AndroidManifest.xml 文件来存储权限,我需要先创建它吗?我正在使用世博会应用程序。
  • 具体请参考展会文档:docs.expo.io/versions/latest/sdk/permissions

标签: react-native expo react-native-image-picker


【解决方案1】:

如果您使用 Expo,则不必使用它。而且您需要存储空间的权利。

您可以运行expo install expo-image-picker expo-permissions 并获得权限Permissions.CAMERA_ROL

示例

import * as React from 'react';
import { Button, Image, View } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import * as Permissions from 'expo-permissions';
import * as Constants from 'expo-Constants';

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

  render() {
    let { image } = this.state;

    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Button
          title="Pick an image from camera roll"
          onPress={this._pickImage}
        />
        {image &&
          <Image source={{ uri: image }} style={{ width: 200, height: 200 }} />}
      </View>
    );
  }

  componentDidMount() {
    this.getPermissionAsync();
  }

  getPermissionAsync = async () => {
    if (Constants.platform.ios) {
      const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
      if (status !== 'granted') {
        alert('Sorry, we need camera roll permissions to make this work!');
      }
    }
  }

  _pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [4, 3],
    });

    console.log(result);

    if (!result.cancelled) {
      this.setState({ image: result.uri });
    }
  };
}

【讨论】:

  • 感谢您的帮助。我应该用这个替换我的代码吗? @hong 开发
  • @RJDdev 此代码是我创建的一个简单示例,旨在帮助您,因为您使用的是 Expo。对你有帮助的可以参考一下。
  • 我不能投票,因为我刚接触 stackoverflow,虽然我刚刚勾选了它。我尝试从您的示例中添加代码以获得权限,并且我已经用新代码更新了我的问题。我收到错误消息:标识符“ImagePicker”已经被声明,所以我的代码可能仍然有问题。 @hong 开发
  • @RJDdev 您调用的模块与您定义的类名相同。电脑出错是因为它认为它重新定义了你引入的模块。
  • @hong-developer 我明白了,我已经更改了类名和导入语句,并用我看到的错误更新了我的问题。 ImagePicker 在我的代码中多次声明,但我认为我不应该在其他地方更改它,因为我认为它指的是“expo-image-picker”。
【解决方案2】:

由于某种原因,这对我不起作用:

Permissions.askAsync(Permissions.CAMERA_ROLL);

我不断收到以下错误:

Unhandled promise rejection: Error: Failed to get permissions

所以,最终对我有用的是,这个函数用于从媒体库中选择图像:

const showImagePicker = async () => {
// Ask the user for the permission to access the media library 
const permissionResult = await ImagePicker.requestMediaLibraryPermissionsAsync();

if (permissionResult.granted === false) {
  alert("You've refused to allow this appp to access your photos!");
  return;
}

const result = await ImagePicker.launchImageLibraryAsync();

// Explore the result
console.log(result);

if (!result.cancelled) {
  setPickedImagePath(result.uri);
      console.log(result.uri);
    }
  }

此功能用于用相机拍照:

  const openCamera = async () => {
    // Ask the user for the permission to access the camera
    const permissionResult = await ImagePicker.requestCameraPermissionsAsync();

    if (permissionResult.granted === false) {
      alert("You've refused to allow this appp to access your camera!");
      return;
    }

    const result = await ImagePicker.launchCameraAsync();

    // Explore the result
    console.log(result);

    if (!result.cancelled) {
      setPickedImagePath(result.uri);
      console.log(result.uri);
    }
  }

在此处获取完整代码和完整说明:https://www.kindacode.com/article/image-picker-in-react-native/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 2022-07-16
    • 2011-10-22
    • 2020-06-29
    • 1970-01-01
    相关资源
    最近更新 更多