i根据文档安装了反应本形图像挑战器。当我尝试从手机中选择图像(击中按钮后(时,模拟器给我这个错误 - null is not an object (evaluating 'ImagePickerManager.showImagePicker')
我的React本地版本是0.59.8
和Image Picker的版本为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>
);
}
}
i将图像拾取器的版本降级到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 ..
停止并重新运行您的包装器或Bundler
然后重置您的缓存
npm start -reset-cache
不是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,
});
}
});
在Android Studio中构建项目,如果您遇到一个错误:
this.options.savetophotos&amp;&amp;build.version.sdk_int&lt; = build.version_codes.p&amp;&amp;....
在您的 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'
}
}
}