最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native Image Picker: Cannot read property 'showImagePicker' of undefined - Stack Overflow

programmeradmin1浏览0评论

I'm following the example exactly as shown in using react-native-image-picker, via terminal with react-native run-ios and coding via Atom editor.

I did npm install react-native-image-picker@latest --save and currently the dependency in package.json shows: "react-native-image-picker": "^0.22.8",

And I clicked a button to trigger the imagePicker but I am getting the error: Cannot read property 'showImagePicker' of undefined.

What am I doing wrong?

Here is the code

import ImagePicker from 'react-native-image-picker'
var Platform = require('react-native').Platform

var options = {
  title: 'Select Avatar',
  customButtons: [
    {name: 'fb', title: 'Choose Photo from Facebook'},
  ],
  storageOptions: {
    skipBackup: true,
    path: 'images'
  }
}

export default class chooseImage extends Component {
  constructor() {
    super()

    this.state = {
      avatarSource: "",
    }
  }

  _uploadImage() {
    ImagePicker.showImagePicker(options, (response) => {
      console.log('Response = ', 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: ', response.customButton);
      }
      else {
        // You can display the image using either data...
        const source = {uri: 'data:image/jpeg;base64,' + response.data, isStatic: true};

        // or a reference to the platform specific asset location
        if (Platform.OS === 'ios') {
          const source = {uri: response.uri.replace('file://', ''), isStatic: true};
        } else {
          const source = {uri: response.uri, isStatic: true};
        }

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

  render() {

    return (
      <View style={styles.container}>
          <TouchableHighlight
            onPress={this._uploadImage}
            underlayColor='transparent'
          >
            <Image
              source={this.state.avatarSource} style={styles.uploadAvatar}
            />
          </TouchableHighlight>
      </View>
    )
  }
}

EDIT

When I console.log(ImagePicker)

I'm following the example exactly as shown in https://github.com/marcshilling/react-native-image-picker using react-native-image-picker, via terminal with react-native run-ios and coding via Atom editor.

I did npm install react-native-image-picker@latest --save and currently the dependency in package.json shows: "react-native-image-picker": "^0.22.8",

And I clicked a button to trigger the imagePicker but I am getting the error: Cannot read property 'showImagePicker' of undefined.

What am I doing wrong?

Here is the code

import ImagePicker from 'react-native-image-picker'
var Platform = require('react-native').Platform

var options = {
  title: 'Select Avatar',
  customButtons: [
    {name: 'fb', title: 'Choose Photo from Facebook'},
  ],
  storageOptions: {
    skipBackup: true,
    path: 'images'
  }
}

export default class chooseImage extends Component {
  constructor() {
    super()

    this.state = {
      avatarSource: "",
    }
  }

  _uploadImage() {
    ImagePicker.showImagePicker(options, (response) => {
      console.log('Response = ', 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: ', response.customButton);
      }
      else {
        // You can display the image using either data...
        const source = {uri: 'data:image/jpeg;base64,' + response.data, isStatic: true};

        // or a reference to the platform specific asset location
        if (Platform.OS === 'ios') {
          const source = {uri: response.uri.replace('file://', ''), isStatic: true};
        } else {
          const source = {uri: response.uri, isStatic: true};
        }

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

  render() {

    return (
      <View style={styles.container}>
          <TouchableHighlight
            onPress={this._uploadImage}
            underlayColor='transparent'
          >
            <Image
              source={this.state.avatarSource} style={styles.uploadAvatar}
            />
          </TouchableHighlight>
      </View>
    )
  }
}

EDIT

When I console.log(ImagePicker)

Share Improve this question edited Sep 24, 2016 at 17:46 Jo Ko asked Sep 24, 2016 at 7:22 Jo KoJo Ko 7,57516 gold badges68 silver badges127 bronze badges 2
  • did you the platform-specific installation stuff, like: github.com/marcshilling/react-native-image-picker#ios , and: github.com/marcshilling/react-native-image-picker#android – Ivan Chernykh Commented Sep 24, 2016 at 9:14
  • @Cherniv I am doing React Native via Atom editor and terminal and not xcode. In my other project, all I had to do was npm install and it worked. – Jo Ko Commented Sep 24, 2016 at 17:47
Add a comment  | 

12 Answers 12

Reset to default 3
  • use manual installation

  • set permission

/AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

cannot read property y of undefined means you have a statement x.y such that x is undefined. in this case, x is ImagePicker. My best guess is that your import statement causes the issue. try this:

var ImagePicker = require('react-native-image-picker');

instead of

import ImagePicker from 'react-native-image-picker'

Which is also what the example suggests.

if you want to stick with the import statement,

try this:

import * as ImagePicker from 'react-native-image-picker'

Which imports the module as ImagePicker and puts it inside the scope. For more info, see: MDN on import statement

I have been working for hours to solve this problem! Finally after adding correctly these lines in MainApplication.java it worked as a charm. Do not forget to rebuild the project.

it seems like IMagePicker is not defined.

Have you tried using

var ImagePicker = require('react-native-image-picker');

instead of the first line?

Hi there are few things that you can try: Run react-native link

Make sure you already completed this step http://prntscr.com/hftr17

If not: your last chance is remove completely your code. Clone a new one.

I was successfully make it work by follow those.

Hope it can give you something.

Rebuild your app. Run the command react-native run-ios and react-native run-android.

Rebuilding your app will just mean you'll get the same error.

You need to go through Xcode for iOS and edit your settings.gradle manually for this to work:

https://github.com/react-community/react-native-image-picker

You don't need to do the steps for both Android and iOS, but the steps outlined for either platform are crucial

So unless you have already executed react-native link, you will need to go through each numbered steps and make sure your binaries are properly linked.

If you are using xcode and getting this error on simulator or real device make sure after installing and react link command you follow manual instructions. as described ie.

  1. In the XCode's "Project navigator", right click on your project's Libraries folder ➜ Add Files to <...> .

  2. Go to node_modules ➜ react-native-image-picker ➜ ios ➜ select RNImagePicker.xcodeproj

  3. Add RNImagePicker.a to Build Phases -> Link Binary With Libraries
  4. For iOS 10+, Add the NSPhotoLibraryUsageDescription, NSCameraUsageDescription, NSPhotoLibraryAddUsageDescription and NSMicrophoneUsageDescription (if allowing video) keys to your Info.plist with strings describing why your app needs these permissions.

Note. = RNImagePicker.a is now libRNImagePicker.a and make sure you add this in your project build phase the lib's build phase itself.

Then you may compile and run it.

Note : After installation you have to restart the project as react-native run-ios or react-native run-android

May be u installed the picker but didn't restart the packager

: )

If you are using xcode and getting this error on simulator or real device make sure after installing and react link command you follow manual instructions. as described ie.

In the XCode's "Project navigator", right click on your project's Libraries folder ➜ Add Files to <...> .

Go to node_modules ➜ react-native-image-picker ➜ ios ➜ select RNImagePicker.xcodeproj

Add RNImagePicker.a to Build Phases -> Link Binary With Libraries For iOS 10+, Add the NSPhotoLibraryUsageDescription, NSCameraUsageDescription, NSPhotoLibraryAddUsageDescription and NSMicrophoneUsageDescription (if allowing video) keys to your Info.plist with strings describing why your app needs these permissions. Note. = RNImagePicker.a is now libRNImagePicker.a and make sure you add this in your project build phase the lib's build phase itself.

Then you may compile and run it.

As per your first comment you mentioned that you are not using Xcode you are using Atom and Terminal. In most of the cases only npm install will be enough but there are some platform specific cases that can be achieved either by linking it using react-native link or by following the manual installation steps provided in the document. if you are not using Xcode then its not possible for you to link this library completely for IOS platform. Make sure you use Xcode and follow the manual steps or else react-native link to solve your issue.

I had the same problem and in iOS it was due to the permission missing, even in the simulator. See the permissions here:

https://github.com/react-native-community/react-native-image-picker/blob/master/docs/Install.md

I just missed "NSPhotoLibraryAddUsageDescription" and it was enough to get the "Cannot read property 'showImagePicker' of undefined" error.

发布评论

评论列表(0)

  1. 暂无评论