Reputation: 1646
I installed react-native-image-picker according to the documentation.
When I am trying to select the image from the phone (after hitting the button), the emulator is giving me this error-
null is not an object (evaluating 'ImagePickerManager.showImagePicker')
My React native's version is 0.59.8
and image picker's version is 0.28.0
this the code-
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
Image,
Button
} from 'react-native';
import ImagePicker from "react-native-image-picker";
export default class App extends Component {
state = {
pickedImage: null
}
reset = () => {
this.setState({
pickedImage: null
});
}
pickImageHandler = () => {
ImagePicker.showImagePicker({title: "Pick an Image", maxWidth: 800, maxHeight: 600}, res => {
if (res.didCancel) {
console.log("User cancelled!");
} else if (res.error) {
console.log("Error", res.error);
} else {
this.setState({
pickedImage: { uri: res.uri }
});
}
});
}
resetHandler = () =>{
this.reset();
}
render() {
return (
<View style={styles.container}>
<Text style={styles.textStyle}>Pick Image From Camera and Gallery </Text>
<View style={styles.placeholder}>
<Image source={this.state.pickedImage} style={styles.previewImage} />
</View>
<View style={styles.button}>
<Button title="Pick Image" onPress={this.pickImageHandler} />
<Button title="Reset" onPress={this.resetHandler} />
</View>
</View>
);
}
}
Upvotes: 5
Views: 19350
Reputation: 61
Build the project in android studio and if you are encountering an error like:
this.options.saveToPhotos && Build.VERSION.SDK_INT <= Build.VERSION_CODES.P && ....
in your projectName/android/build.gradle update these
ext {
buildToolsVersion = "29.0.2"
minSdkVersion = 21
compileSdkVersion = 29
targetSdkVersion = 29
}
and also update this
afterEvaluate {
project -> if (project.hasProperty("android")) {
android {
compileSdkVersion 29
buildToolsVersion '29.0.2'
}
}
}
Upvotes: 0
Reputation: 516
I think there is a bug with the current version. I've experienced this, what I did was to uninstall the current version on the image-picker package, and installed the version @1.1.0
:
Or you can simply downgrade your current version.
npm uninstall react-native-image-picker
npm install [email protected] --save
react-native link react-native-image-picker
cd ios
pod install
cd ..
Stop and re-run your Packager or Bundler
Then reset your cache
npm start --reset-cache
Upvotes: 1
Reputation: 1646
I downgraded image picker's version to 28.0.0
and then rebuilt the app. Now it is working.
Upvotes: 2
Reputation: 13926
Not ImagePickerManager
You can use ImagePicker
Example
import ImagePicker from 'react-native-image-picker';
// More info on all the options is below in the API Reference... just some common use cases shown here
const options = {
title: 'Select Avatar',
customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
storageOptions: {
skipBackup: true,
path: 'images',
},
};
/**
* The first arg is the options object for customization (it can also be null or omitted for default options),
* The second arg is the callback which sends object: response (more info in the API Reference)
*/
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({
avatarSource: source,
});
}
});
Upvotes: 0