3

My feature offers a small image icon as a touchable to upload or take a picture, using "react-native-image-picker": "^3.3.2".

I'm getting error: Cannot read property 'launchImageLibrary' of undefined, same as this GitHub issue but as you can see, my code already have the import as they told to.

Here's my full code:

import React from 'react';
import {
    StyleSheet,
    Image,
    TouchableOpacity,
    Alert
} from 'react-native';
import { launchImageLibrary } from 'react-native-image-picker';

const ImageUpload: React.FC<any> = ({}) => {

    function showMessage() {
        Alert.alert("Upload image", "Choose a option", [
            {
                text: 'Camera',
                onPress: () => openCamera(),
            },
            {
                text: 'Gallery',
                onPress: () => openLibrary()
            },
        ]);
    }

    const openLibrary = () => {

        const options = {
            storageOptions: {
              skipBackup: true,
              path: 'images',
            },
          };
        launchImageLibrary(options, (response) => {
            console.log(response);
        });

    }

    const openCamera = () => {
        //ongoing
    }

    return(
        <>
            <TouchableOpacity onPress={()=>showMessage()}>
                <Image source={require('./picture.png')} style={{ width: 70, height: 70 }}/>
            </TouchableOpacity>
        </>
     );
};

const style = StyleSheet.create({
    ImageIcon: {
        justifyContent: "center",
        alignItems: "center",
    }

});

export default ImageUpload;

Also, In VS Code, I have this error when calling launchImageLibrary: enter image description here

I have already perfomed a npx pod-install.

lsilva
  • 147
  • 2
  • 17

5 Answers5

2

It took me a very long time to get this to work but this worked for me.

< Button onPress = {
  () =>
  ImagePicker.launchImageLibrary({
      mediaType: 'photo',
      includeBase64: false,
      maxHeight: 200,
      maxWidth: 200,
    },
    (response) => {
      console.log(response);
      this.setState({
        resourcePath: response
      });
    },
  )
}
title = "Select Image" / >

References:

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
2

In my case with Android, restarting the emulator fixed the problem. npm run android

Fallen
  • 4,435
  • 2
  • 26
  • 46
0

i use "react-native-image-picker": "^4.8.5",

i just restart metro "npx react-native start" or "npx react-native start --reset-cache"

and rerun metro bundler "npx react-native run-android"

my code is

import { launchImageLibrary } from "react-native-image-picker";

 handleChangePhoto = () => {
    launchImageLibrary({ noData: true }, (response) => {
      console.log(response);
      // if (response) {
      //   setPhoto(response);
      // }
    });
  };
0

Importing it like this solved my issue;

import * as ImagePicker from 'react-native-image-picker';
doneforaiur
  • 1,308
  • 7
  • 14
  • 21
-4
import * as ImagePicker from 'react-native-image-picker';
Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60