gpt4 book ai didi

react-native - 在 React Native 中,如何访问 native 相机应用程序?

转载 作者:行者123 更新时间:2023-12-03 21:22:23 26 4
gpt4 key购买 nike

我想添加一个链接到我的应用程序,以打开手机的原生相机应用程序?这可能吗?

我知道 react-native-camera 存在,但从文档看来,它似乎只支持访问相机,以便在您的应用程序中创建自己的相机界面。我宁愿只使用手机上已有的相机应用程序。

谢谢

最佳答案

使用 react-native-image-picker ,您可以访问手机原生的相机。

[编辑]
示例代码

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>
);
}
const styles = StyleSheet.create({
container: {
alignItems:"center"
},
textStyle: {
fontWeight:"bold",
fontSize:30,
textAlign:"center",
color:"red",
marginTop:10
},
placeholder: {
borderWidth: 1,
borderColor: "black",
backgroundColor: "#eee",
width: "70%",
height: 280,
marginTop:50,
},
button: {
width: "80%",
marginTop:20,
flexDirection:"row",
justifyContent: "space-around"
},
previewImage: {
width: "100%",
height: "100%"
}
});
}

来源: react-native-image-picker

关于react-native - 在 React Native 中,如何访问 native 相机应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52321171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com