Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native Expo Imagepicker请求权限,但没有其他操作_React Native_Expo_React Native Image Picker - Fatal编程技术网

React native Expo Imagepicker请求权限,但没有其他操作

React native Expo Imagepicker请求权限,但没有其他操作,react-native,expo,react-native-image-picker,React Native,Expo,React Native Image Picker,我正在使用expo构建一个react原生应用程序。我正在尝试实现expo imagePicker,这样用户就可以从他们的图库或相机卷中选择一幅图像,并在应用程序上使用它。但是,在imagePicker ahs请求权限后,我允许了他们,没有其他事情发生。用户不会被带到他们的画廊或照相馆。目前没有错误,但是imagePicker无法正常工作。 我需要对代码进行哪些更改 ImageSelector.js文件 import * as React from 'react'; import { Button

我正在使用expo构建一个react原生应用程序。我正在尝试实现expo imagePicker,这样用户就可以从他们的图库或相机卷中选择一幅图像,并在应用程序上使用它。但是,在imagePicker ahs请求权限后,我允许了他们,没有其他事情发生。用户不会被带到他们的画廊或照相馆。目前没有错误,但是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 });
    }
  };
}
import*as React from'React';
从“react native”导入{按钮、图像、视图};
从“世博会图像采集器”导入*作为图像采集器;
从“expo Permissions”导入*作为权限;
从“expo常量”导入*作为常量;
导出默认类ImageSelector扩展React.Component{
状态={
图像:空,
};
render(){
设{image}=this.state;
返回(
{图像&&
}
);
}
componentDidMount(){
此参数为.getPermissionAsync();
}
getPermissionAsync=async()=>{
if(Constants.platform.android){
const{status}=wait Permissions.askAsync(Permissions.CAMERA_ROLL);
如果(状态!=“已授予”){
警报('抱歉,我们需要摄像头滚动权限才能执行此操作!');
}
}
}
_pickImage=async()=>{
让结果=等待ImagePicker.launchImageLibraryAsync({
mediaTypes:ImagePicker.MediaTypeOptions.All,
允许编辑:对,
相位:[4,3],
});
控制台日志(结果);
如果(!result.cancelled){
this.setState({image:result.uri});
}
};
}

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

您可以运行expo安装expo image picker expo permissions并获得权限

示例

import*as React from'React';
从“react native”导入{按钮、图像、视图};
从“世博会图像采集器”导入*作为图像采集器;
从“expo Permissions”导入*作为权限;
从“expo常量”导入*作为常量;
导出默认类ImagePickerExample扩展React.Component{
状态={
图像:空,
};
render(){
设{image}=this.state;
返回(
{图像&&
}
);
}
componentDidMount(){
此参数为.getPermissionAsync();
}
getPermissionAsync=async()=>{
if(Constants.platform.ios){
const{status}=wait Permissions.askAsync(Permissions.CAMERA_ROLL);
如果(状态!=“已授予”){
警报('抱歉,我们需要摄像头滚动权限才能执行此操作!');
}
}
}
_pickImage=async()=>{
让结果=等待ImagePicker.launchImageLibraryAsync({
mediaTypes:ImagePicker.MediaTypeOptions.All,
允许编辑:对,
相位:[4,3],
});
控制台日志(结果);
如果(!result.cancelled){
this.setState({image:result.uri});
}
};
}

您是否拥有所有权限?正如文档所说,“您还需要在iOS上添加UsageDescription,在Android上添加一些权限,请参阅安装文档。”@SujilMaharjan我还没有AndroidManifest.xml文件来存储权限,我需要先创建这个文件吗?我正在使用expo应用程序。请参阅expo文档:谢谢你的帮助。我应该用这个替换我的代码吗@商行develop@RJDdev这段代码是一个简单的示例,我创建它是为了帮助您,因为您正在使用Expo。你可以参考对你有帮助的内容。我不能投票,因为我是stackoverflow的新手,尽管我刚刚勾选了它。为了获得权限,我尝试添加您示例中的代码,并用新代码更新了我的问题。我得到了一个错误:标识符“ImagePicker”已经声明,所以我的代码可能仍然有问题@商行develop@RJDdev您调用的模块具有与您定义的相同的类名。计算机出错是因为它认为它重新定义了您引入的模块。@我知道了,我已经更改了类名和导入语句,并用我看到的错误更新了我的问题。在我的代码中还有很多次提到ImagePicker,但我认为我不应该在其他地方更改它,因为我认为它指的是“世博会图像选择器”。