如何使用React Native文件系统访问设备文件夹

时间:2020-07-11 01:48:23

标签: react-native react-native-android react-native-fs

我需要从使用rncamera roll应用程序创建的自定义文件夹中获取文件,我使用react-native-fs访问该文件夹,但是即使我正确指定了文件夹名称,也无法获取文件,但是我得到可能的未处理的承诺拒绝(id:7):错误:文件夹不存在。如何访问此文件夹?

即使删除文件夹名称RNFS.readDir(RNFS.ExternalStorageDirectoryPath)来检查console.log结果,我也收到错误“ isFile不是函数”。

此代码有什么问题,我该如何纠正。

UNSAFE_componentWillMount() {
        RNFS.readDir(RNFS.ExternalStorageDirectoryPath+"myApp Videos")
        .then((result) => {
        console.log('GOT RESULT', result);
        return Promise.all([RNFS.stat(result[0].path), result[0].path]);
        })
        .then((statResult) => {
        let videos = []
        var allowedExtensions = /(\.avi|\.mp4|\.mov|\.wmv|\.avi)$/i;
        statResult.forEach(item => {
        if (item.isFile() && !allowedExtensions.exec(item.originalFilepath)) {
        videos.push(item)
        }
        });
        console.log(videos)
        })
    }

    setIndex = (index) => {
        if (index === this.state.index) {
            index = null
        }
        this.setState({ index })
    }
   

render() {
    return (
        <View style={styles.container}>
            <ScrollView
                contentContainerStyle = {styles.scrollview}
                {
                    ...this.state.videos && this.state.videos.length > 0 && this.state.videos.map((p, i) => {
                        const isSelected = i === this.state.index;
                        const divide = isSelected && this.share === true ? 1 : 3;
                        return(
                            <Video
                                source={{uri: videos}}
                                style={{opacity: i === this.state.index ? 0.5 : 1, width: width/divide, height: width/divide}}
                                key={i}
                                underlayColor='transparent'
                                onPress={() => this.setIndex(i)}
                                ref={ref => {
                                    this.player = ref;
                                  }} // Store reference
                                  onError={this.videoError} // Callback when video cannot be loaded
                            />

                            
                        )
                    })
                }
            >

            </ScrollView>
        </View>
    );
}

0 个答案:

没有答案
相关问题