我已经成功安装了react-native-image-picker,对于一个新的react本地应用程序,将其链接并通过info.plist文件授予了权限,以访问相机,照片等...
我正在react-native-image-picker页面上使用自述文件中的代码,但收到错误
当尝试打开openGallery()时,出现以下警告,并且没有图像库打开:
可能未处理的Promise Rejection TypeError;未定义不是函数(在... reactNativeImagePicker.default.launchImageLibrary附近)
这是我的代码:
import ImagePicker from 'react-native-image-picker';
.....
class App extends Component {
constructor(props) {
super(props)
this.state = {
fileURL: '',
}
}
//function
openGallery = async () => {
const options = {
mediaType: 'video'
}
ImagePicker.launchImageLibrary(options, (response) => {
console('Response = ', response);
if(response.didCancel){
console.log('User cancelled image picker');
}
else{
this.setState({ fileURL: response.uri });
}
});
}
render() {
return (
<View style={styles.container}>
<Button
style={{margin: 20}}
onPress={this.openGallery}
title='Open Gallery'
backgroundColor='blue'
/>
</View>
);
}
}
运行工厂锅炉板代码。怎么了?
@BoredKid,我在控制台日志中得到它:
对于(ImagePicker):
{showImagePicker: ƒ}
showImagePicker: ƒ showImagePicker(options, callback)
__proto__: Object
用于(ImagePicker.showImagePicker);
ƒ showImagePicker(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
return ImagePickerManager.showImagePicker((0, _objectS…
答案 0 :(得分:2)
该模块没有默认导出。您需要使用命名导出:
import * as ImagePicker from 'react-native-image-picker';
或
import {launchImageLibrary} from 'react-native-image-picker';
答案 1 :(得分:1)
已更新为新输入:您的ImagePicker
对象没有launchImageLibrary
或launchCamera
。您的安装有问题...可能是安装无法正常进行,或者您弄错了一个步骤。让我们尝试重新安装,我们会发现问题仍然存在
答案 2 :(得分:0)
代替写作
import { ImagePicker } from 'react-native-image-picker',
您应该声明(在同一位置)...
var ImagePicker = require('react-native-image-picker');
这对我有用。
答案 3 :(得分:0)
我通过安装最新版本解决了这个错误,对我来说是
"react-native-image-picker": "^3.3.2",
并且还使用存储库中的示例代码: https://github.com/react-native-image-picker/react-native-image-picker/blob/main/example/src/App.js
import React, {useState, useEffect} from 'react';
import {StyleSheet, Text, View, Image} from 'react-native';
import cameraImage from '../../../../assets/icons/camera.png';
import galleryImage from '../../../../assets/icons//gallery.png';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';
import * as ImagePicker from 'react-native-image-picker';
import {PermissionsAndroid} from 'react-native';
const ImagePicker = () => {
const [responseCamera, setResponseCamera] = React.useState(null);
const [responseGallery, setResponseGallery] = React.useState(null);
const openCameraWithPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: 'App Camera Permission',
message: 'App needs access to your camera ',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
ImagePicker.launchCamera(
{
mediaType: 'photo',
includeBase64: false,
maxHeight: 200,
maxWidth: 200,
},
(response) => {
console.log(response);
setResponseCamera(response);
setResponseGallery(null);
},
);
} else {
console.log('Camera permission denied');
}
} catch (err) {
console.warn(err);
}
};
return (
<View
style={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-around',
margin: 4,
}}>
<TouchableOpacity onPress={() => openCameraWithPermission()}>
{responseCamera === null ? (
<Image style={styles.icon} source={cameraImage} />
) : (
<Image style={styles.icon} source={{uri: responseCamera.uri}} />
)}
</TouchableOpacity>
<TouchableOpacity
onPress={() =>
ImagePicker.launchImageLibrary(
{
mediaType: 'photo',
includeBase64: false,
maxHeight: 200,
maxWidth: 200,
},
(response) => {
setResponseGallery(response);
setResponseCamera(null);
},
)
}>
{responseGallery === null ? (
<Image style={styles.icon} source={galleryImage} />
) : (
<Image style={styles.icon} source={{uri: responseGallery.uri}} />
)}
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
icon: {
height: 50,
width: 50,
},
});
export default ImagePicker;
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />