react-native 文件系统找不到应用程序文件

react-native filesystem can't find app files

我正在使用 react-native 构建一个应用程序,我正在尝试使用 react-native-fs 模块列出位于应用程序文件夹中的一系列图像。这些图像位于名为 'data' 的应用程序项目文件夹中的一个文件夹中,因此,例如,如果我想显示其中一张图像,可以这样做:

<Image source={require('./data/boo.png')} />

然而,当我尝试使用 react-native-fs 列出该文件夹中的所有文件时,如下所示:

  RNFS.readdir(RNFS.DocumentDirectoryPath+'/data') 
  .then((result) => {
    console.log('GOT RESULT', result);
  })
  .catch((err) => {
    console.log(err.message, err.code);
  });

我收到错误 'Folder does not exist'。此外,当我删除 +'/data' 时,列出的唯一结果是一个名为 'ReactNativeDevBundle.js' 的文件,其路径为 '/data/user/0/com.awesomeproject/files/ReactNativeDevBundle.js'。这是预期的行为吗?如果这是预期的行为,而我做错了什么,我如何从应用程序中访问我想要的文件夹?附带问题,如果我想为该图像标签提供绝对路径,那会是什么样子。

首先,您是否在 运行 时间内创建数据文件夹?或者你为什么认为那是文件所在的地方?

第二,

Also when I remove the +'/data' the only result listed is a file by the name of 'ReactNativeDevBundle.js', with a path of '/data/user/0/com.awesomeproject/files/ReactNativeDevBundle.js'. Is this the expected behavior?

是的,这是预期的行为,RNFS.DocumentDirectoryPath 直接进入 /data/user/0/com.awesomeproject/files/,如果您想继续使用,您应该在此处创建数据文件夹您当前拥有的相同代码

编辑: 根据包的贡献者之一:if your folder is within the javascript-space this package won't work.

If you're using android, you may need to put the files into the assets-folder within the android-folder. Then you should be able to use readDirAssets.

我推荐阅读Differences between File Source

摘录:

Normal Files: These files are created by your app using fs, or fetch API, you can do any operation on these files.

Asset Files: Compiled into the app bundle so generally they're on readonly mode