android - react native 图像选择器 showimagepicker

标签 android npm react-native

我想使用 react-native 图像选择器拍摄图像, 我正在使用 npm run android 命令,但是当应用程序在 expo 中运行时,它显示此错误:

undefined is not an object(evaluating 'imagepickerManager.showimagepicker')

因为我正在使用 expo 项目 index.android.js 不存在于项目中所以 react-native run-android 命令不起作用。

有人可以指导我如何恢复此错误吗?

ERROR

import React, {Component} from 'react';
import {Text, View, TouchableOpacity, Image} from 'react-native';

// var ImagePicker = require('react-native-image-picker');
import * as ImagePicker from 'react-native-image-picker';
// More info on all the options is below in the README...just some common use cases shown here
var options = {
  title: 'Select Avatar',
  customButtons: [
    {name: 'fb', title: 'Choose Photo from Facebook :'},
  ],
  storageOptions: {
    skipBackup: true,
    path: 'images'
  }
};

export default class App extends Component{
  constructor(props){
    super(props);
    this.state={
      avatarSource:null
    }
  }
  render(){
    let img=this.state.avatarSource == null?null:
    <Image
    source={this.state.avatarSource}
    style={{height:200, width:300}}
    />
    return(
        <View>
        <Text>Welcome to Image Picker</Text>
          <TouchableOpacity onPress = {this.show.bind()} >
          <Text>Load Images</Text>
          </TouchableOpacity>
          {img}
        </View>
    )
  }
  show(){
          ImagePicker.showImagePicker(options, (response) => {
        if (response.didCancel) {
          console.log('User cancelled image picker');
        }
        else if (response.error) {
          console.log('ImagePicker Error: ', response.error);
        }
        else if (response.customButton) {
          console.log('User tapped custom button please: ', response.customButton);
        }
        else {
          let source = { uri: response.uri };

          // You can also display the image using data:
          // let source = { uri: 'data:image/jpeg;base64,' + response.data };

          this.setState({
            avatarSource: source
          });
        }
      });
  }
}

最佳答案

我遇到了同样的问题。并通过以下方式解决 -

  1. react-native 链接 react-native-image-picker
  2. 运行 react-native-run-ios 和 react-native run-android 命令

More description here

关于android - react native 图像选择器 showimagepicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43994281/

相关文章:

android - 如何使用 Android 应用程序中的 "Azure IoT Hub Device Provisioning Service"注册设备并将遥测读数发送到 Azure IoT 中心?

react-native - 仅在 react-native MapView 上设置初始区域

arrays - react-native-multiple-select 存储提交时选择的项目

java - onActivityResult 的行为不符合预期

android - 如何在不更改 MainActivity 标题的情况下更改 fragment 的标题?

java - 如何在 Android 类中初始化变量?

javascript - 引用错误 : process is not defined when requiring Shelljs in ReactJS

node.js - 为什么 OpenShift 上的 Ghost node_modules 比本地大得多?

npm 发布早期主要版本的补丁

android - React Native 中 AsyncStorage 的键的命名约定是什么?