1

我正在尝试按照 React Native 文档访问Camera Roll。我尝试将相机胶卷图像包装在Modal Component中。当我按下“打开相机胶卷”按钮时,Modal从底部出现“关闭模式”按钮可见,但没有来自相机胶卷的图像。

当我没有将它包裹ModalModal.

此外,如果您能给我一些关于如何使图像可选择并以每行 3 张图像显示的提示,我们将不胜感激。

    import React from 'react';
    import { CameraRoll, Image, Modal, ScrollView, Text, View } from 'react-native';
    import { Button } from 'react-native-elements';

    export default class AddEvent extends React.Component {
        constructor(props){
            super(props)
            this.state = {
                modalVisible: false,
                photos: [],
            }
        }

        getPhotos = () => {
            CameraRoll.getPhotos({
                first: 100,
            })
            .then(r => this.setState({ photos: r.edges }))
            .catch((err) => {
                alert('Error loading camera roll');
                return;
            });
        }

        openModal() {
            this.getPhotos();
            this.setState({modalVisible:true});
        }

        closeModal() {this.setState({modalVisible:false});}

        static navigationOptions = ({ navigation }) => {
            return {
                headerTitle: (<Text>Camera Roll Test</Text>),
            }
        };

        render(){
        return (
            <View>
                <Modal
                    visible={this.state.modalVisible}
                    animationType={'slide'}
                    onRequestClose={() => this.closeModal()}>
                    <ScrollView>
                        {this.state.photos.map((p, i) => {
                            return (
                                <Image
                                    key={i}
                                    style={{width: 300, height: 100,}}
                                    source={{ uri: p.node.image.uri }}/>
                            );
                        })}
                    </ScrollView>
                    <Button
                        onPress={() => this.closeModal()}
                        title="Close modal"/>
                </Modal>
                <Button
                    onPress={() => this.openModal()}/>
            </View>
        );
    }
}
4

2 回答 2

2

明白了,flex: 1风格<ScrollView>contentContainerStyle. 看起来不是很好,但照片显示。归功于 reddit 上的 u/Mingli91。

import React from 'react';
import { CameraRoll, Image, Modal, ScrollView, StyleSheet, Text, View } from 'react-native';
import { Button } from 'react-native-elements';


const Dimensions = require('Dimensions');
const window = Dimensions.get('window');
const screenWidth = window.width;
const screenHeight = window.height;


export default class AddEvent extends React.Component {
    constructor(props){
        super(props)
        this.state = {
            modalVisible: false,
            photos: [],
        }
    }

    getPhotos = () => {
        CameraRoll.getPhotos({
            first: 100,
        })
        .then(r => this.setState({ photos: r.edges }))
        .catch((err) => {
            alert('Error loading camera roll');
            return;
        });
    }

    openModal() {
        this.getPhotos();
        this.setState({modalVisible:true});
    }

    closeModal() {this.setState({modalVisible:false});}

    static navigationOptions = ({ navigation }) => {
        return {
            headerTitle: (<Text>Camera Roll Test</Text>),
        }
    };

    render(){
        return (
            <View>
                <Modal style={styles.modal}
                    visible={this.state.modalVisible}
                    animationType={'slide'}
                    onRequestClose={() => this.closeModal()}>
                    <ScrollView style={{flex: 1}}
                        contentContainerStyle={{ height: 100, width: 300 }}>
                        {this.state.photos.map((p, i) => {
                            return (
                                <Image
                                    key={i}
                                    style={{width: 300, height: 100,}}
                                    source={{ uri: p.node.image.uri }}/>
                            );
                        })}
                    </ScrollView>
                    <Button
                        onPress={() => this.closeModal()}
                        title="Close modal"/>
                </Modal>
                <Button
                    onPress={() => this.openModal()}/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    modal: {
        flex: 1,
        alignItems: 'center',
        width: screenWidth,
        height: screenHeight,
    }
});
于 2018-02-10T06:44:13.457 回答
0

安卓 10

如果您开始在 Android 10 上注意到这一点,请尝试将其添加到您的AndroidManifest.xml

<application
  ...
  android:requestLegacyExternalStorage="true" />

Android 正在转向“范围存储”。他们开始在 Android 10 中使用此功能,并可选择通过添加android:requestLegacyExternalStorage="true"到您的AndroidManifest.xml文件来选择退出。

但是,从 Android 11 开始,这是强制更改,您的值android:requestLegacyExternalStorage将被忽略。

在这里阅读更多:

https://developer.android.com/training/data-storage/use-cases#opt-out-scoped-storage

在这里发帖是因为这是我们的问题,而且很难找到。

于 2020-12-23T22:15:04.047 回答