简体   繁体   中英

Where to put file in react-native project, so i can access it with react-native-fs (Android)

I'm working on a react-native project. I have a file that i want to access using the react-native-fs library. The file is called "chart.html", and i want to get its path with something like this:

'file://' + RNFS.DocumentDirectoryPath + '/chart.html';

I have already tried putting the file in the root directory, android/app, android/app/src and android/app/src/main, without success so far.

Where do i put the file so the code from above works? Thanks.

If you place your files into the " assets " folder and recompile the Android-project, you will be able to access these files with react-native-fs :

android/app/src/main/assets/

The following methods might help you to access them:

You can also have a look at " existsAssets " and " copyFileAssets ", the latter for putting the files into Document-Folder if other libraries shall work with the files without having access to the assets-folder itself.

You need to use react-native-asset to put files to your project and access them with require keyword.

Example for file with path ./assets/html/chart.html :

  1. Add or modify react-native.config.js
module.exports = {
 project: {
   ios: {},
   android: {},
 },
 dependencies: {
 },
 assets: [
   './assets/html'
 ],
};
  1. Run npx react-native-asset
  2. Use the file:
const file = require('./assets/html/chart.html')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM