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

问题描述 投票:0回答:1

我需要从使用 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>
    );
}

react-native react-native-fs
1个回答
0
投票

我找到了解决此类问题的解决方案,通过保证访问并在代码中添加控件,使用 React Native 和 RNFS => 访问私有文件和目录,如下所示:

这里是如何将数据库文件从 db 文件夹复制到ExternalStorageDirectory 的示例

var path_dbMaster="/data/user/0/com.<name_your_package>/databases/db_name"
  var path_dbCopie=RNFS.ExternalStorageDirectoryPath+'/dbCopie.db'

  const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, );
       if (granted === PermissionsAndroid.RESULTS.GRANTED)
        {   
          
          RNFS.copyFile(path_dbMaster,path_dbCopie)  
          .then(result => {  
            console.log('file copied:', result);
          })
          .catch(err => {
            console.log(err);
          });
          
           // or any treatment you have to do
         } 
        else {      
           console.log('not granted');     
           
           Alert.alert(''," Permission required ",[{
            text:'OK',
            style:'cancel'
           }])

          }

© www.soinside.com 2019 - 2024. All rights reserved.