Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[馃悰] Android: Cannot read property 'showImagePicker' of undefined #2255

Open
DeveloperMCD opened this issue Dec 20, 2023 · 3 comments
Open

Comments

@DeveloperMCD
Copy link

Description

I run the sample code, the app crashes with this error:
Cannot read property 'showImagePicker' of undefined, js engine: hermes

How to repeat issue and example

I run the sample code, the app crashes with this error:
Cannot read property 'showImagePicker' of undefined, js engine: hermes

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

 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 {
            const source = {uri: response.uri};

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

            this.setState({
                filePath: response,
                fileData: response.data,
                fileUri: response.uri,
            });
        }
    });

Solution

App needs to not crash.

Additional Information

    "react-native-image-picker": "^7.1.0",
    "react-native": "0.72.4",

Android
Linux
VSCode

@DeveloperMCD
Copy link
Author

To put it simply, another way:

Right before ImagePicker.showImagePicker() I put in a couple console.log's

console.log('ImagePicker:');
console.log(ImagePicker);

Which yields:
ImagePicker:
undefined

WHY is the library "undefined", when I'm clearly loading it properly at the top of my React Native file?

@DeveloperMCD
Copy link
Author

Ok, I solved my own problem. I had to look at the code and really dig.

The documentation, getting started guide for this library is TOTALLY useless. You can't even start a blank app and paste in the demo code and get it to work! Observe:

Documentation:
import ImagePicker from 'react-native-image-picker';
ImagePicker.showImagePicker

By that they meant:
import * as ImagePicker from 'react-native-image-picker';
ImagePicker.launchImageLibrary

NOT THE SAME THING. No offense.

@ghost
Copy link

ghost commented Jan 23, 2024

const myLaunchCamera = async () => {
    try {
        const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.CAMERA,
            {
                title: 'Camera Permission',
                message: 'App needs access to your camera.',
                buttonNeutral: 'Ask Me Later',
                buttonNegative: 'Cancel',
                buttonPositive: 'OK',
            },
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            console.log('Camera permission granted');

            const options = {
                mediaType: 'photo',
                quality: 0.7,
            };
        
            launchCamera(options, async (response) => {
                if (response.didCancel) {
                    console.log('Camera launch canceled');
                } else if (response.error) {
                    console.error('Camera launch error', response.error);
                } else {
                    // Handle the captured image (response.uri)
                    console.log(response);


                    // uploadImage(response.uri);
                }
            });


        } else {
            console.log('Camera permission denied');
        }
    } catch (err) {
        console.warn(err);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant