React Native Image Picker:null 不是对象(正在评估 'ImagePickerManager.showImagePicker')
React Native Image Picker: null is not an object (evaluating 'ImagePickerManager.showImagePicker')
我按照文档安装了react-native-image-picker。
当我尝试 select 来自 phone 的图像时(按下按钮后),模拟器给我这个错误-
null is not an object (evaluating 'ImagePickerManager.showImagePicker')
我的 React native 版本是 0.59.8
图像选择器的版本是 0.28.0
这是代码-
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>
);
}
}
不行ImagePickerManager
你可以用ImagePicker
例子
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,
});
}
});
我将图像选择器的版本降级为 28.0.0
,然后重新构建了应用程序。现在可以使用了。
我认为当前版本存在错误。我遇到过这种情况,我所做的是卸载图像选择器包上的当前版本,并安装版本 @1.1.0
:
或者您可以简单地降级当前版本。
npm uninstall react-native-image-picker
npm install react-native-image-picker@1.1.0 --save
react-native link react-native-image-picker
cd ios
pod install
cd ..
停止并重新运行您的打包程序或捆绑程序
然后重置缓存
npm start --reset-cache
在 android studio 中构建项目,如果您遇到如下错误:
this.options.saveToPhotos && Build.VERSION.SDK_INT <= Build.VERSION_CODES.P && ....
在你的projectName/android/build.gradle更新这些
ext {
buildToolsVersion = "29.0.2"
minSdkVersion = 21
compileSdkVersion = 29
targetSdkVersion = 29
}
并更新这个
afterEvaluate {
project -> if (project.hasProperty("android")) {
android {
compileSdkVersion 29
buildToolsVersion '29.0.2'
}
}
}
我按照文档安装了react-native-image-picker。
当我尝试 select 来自 phone 的图像时(按下按钮后),模拟器给我这个错误-
null is not an object (evaluating 'ImagePickerManager.showImagePicker')
我的 React native 版本是 0.59.8
图像选择器的版本是 0.28.0
这是代码-
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>
);
}
}
不行ImagePickerManager
你可以用ImagePicker
例子
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,
});
}
});
我将图像选择器的版本降级为 28.0.0
,然后重新构建了应用程序。现在可以使用了。
我认为当前版本存在错误。我遇到过这种情况,我所做的是卸载图像选择器包上的当前版本,并安装版本 @1.1.0
:
或者您可以简单地降级当前版本。
npm uninstall react-native-image-picker
npm install react-native-image-picker@1.1.0 --save
react-native link react-native-image-picker
cd ios
pod install
cd ..
停止并重新运行您的打包程序或捆绑程序
然后重置缓存
npm start --reset-cache
在 android studio 中构建项目,如果您遇到如下错误:
this.options.saveToPhotos && Build.VERSION.SDK_INT <= Build.VERSION_CODES.P && ....
在你的projectName/android/build.gradle更新这些
ext {
buildToolsVersion = "29.0.2"
minSdkVersion = 21
compileSdkVersion = 29
targetSdkVersion = 29
}
并更新这个
afterEvaluate {
project -> if (project.hasProperty("android")) {
android {
compileSdkVersion 29
buildToolsVersion '29.0.2'
}
}
}