【问题标题】:react native image picker showimagepicker反应原生图像选择器 showimagepicker
【发布时间】:2017-05-16 06:48:08
【问题描述】:

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

undefined 不是对象(正在评估 'imagepickerManager.showimagepicker')

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

谁能指导我如何恢复这个错误?

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
          });
        }
      });
  }
}

【问题讨论】:

  • 改进的格式和语法

标签: android npm react-native-android


【解决方案1】:

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

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

More description here

【讨论】:

    【解决方案2】:

    我只是通过运行react-native link 来修复此错误,然后重新构建您的应用程序。它应该按预期工作。 :)

    【讨论】:

      【解决方案3】:

      从“react-native-image-picker”导入 * 作为 ImagePicker

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多