react-native-fs: How to read local images (Get base64 of local file)

不问归期 提交于 2019-12-08 12:23:14

问题


I have local files in my app which I read like this:

<Image source={require("../../img/pic1.jpg") />

How to read this file using RNFS.readFile(filepath, "base64"); using RNFS.readFile("../../img/pic1.jpg", "base64"); gives me error "no such file"


回答1:


react-native-fs will only access the files from device storage. So this problem poses a quandary. These files are bundled at build time so how can we access them in development vs production?

My project structure

├── android
├── app
│   └── assets
│       └── images
└── ios

I store my images inside ./app/assets/images/

Development

When in Development or debug the files are served to the device via the metro bundler so they are not actually stored on the device. So accessing them from the devices storage would not be possible. You would have to provide some sort of mocked functionality for when you are developing. It is possible to use __DEV__ to allow different functions to run depending on the environment https://facebook.github.io/react-native/docs/javascript-environment

if (__DEV__) {
  // do something in development
} else {
  // do something in production
}

Production

From some research it does seem like it is possible to access the files in production.

iOS

What I have discovered is that the files reside in a folder called assets that are in the MainBundlePath.

So if you want to access the following file in my local directory ./app/assets/images/image.png you would need the following file path: RNFS.MainBundlePath + /assets/app/assets/images/image.png to access it on the device.

You can check to see the file path by looking at the Packaging.log that is generated with your ipa.

Android

Android doesn't have a folder called MainBundlePath that is only on iOS. I have spent some time researching and looking at the different folders on the device but I was unable to access the images that are bundled. It would seem that they not accessible in the way that we would hope using react-native-fs.

Possible solutions

rn-fetch-blob

rn-fetch-blob has functionality for both ios and Android to read from the assets directory

To access the files for iOS you would do something like this:

RNFetchBlob.fs.stat(fs.dirs.MainBundleDir + '/whatever/files/you/added.mp3').then( stats => {...});

And for Android you would do something like this:

RNFetchBlob.fs.stat(fs.asset('/whatever/files/you/added.mp3').then( stats => {...});

It does give this warning about the assets being compressed by Gradle.

Gradle will compress bundled assets by default. In order to interact with asset files with this module, compression must be disabled. Please see How to access files from assets for more detail.

This could explain why I was unable to find any assets in Android when looking using react-native-fs

You can read more about using rn-fetch-blob and its filesystem here https://github.com/joltup/rn-fetch-blob#user-content-file-system

Storing the files in the native projects

Rather than relying on the bundler to package up the images for you and put them on the device. You could store copies of them already in the iOS and Android projects. This has the bonus that the images would be available in development, however you would storing the images multiple times and it could lead to larger application sizes.

Storing the images as base64

You could store the images as base64 strings in json files and then require these by your application. That would then mean that you would have them already in base64 (so the app doesn't have to do any conversion) but again you would be increasing the size of your application. If it is only a small number of images it may be the easiest option.




回答2:


Just using filename = response.uri.replace('file:', '')

The final code:

if (Platform.OS === 'ios') {
      filename = response.uri.replace('file:', '')
} else {
      filename = response.uri
}

Working now ...



来源:https://stackoverflow.com/questions/54578294/react-native-fs-how-to-read-local-images-get-base64-of-local-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!