Stone
Stone

Reputation: 291

How to read file from assets folder in react-native?

I have some data in android\app\src\main\assets and I am using rn-fetch-blob as filesystem.

But what is the schema path to assets folder in android? How to read data from it?

Is there any other way to ship pre pupolate data file in react-native ? Can I place file in react-native project root directory?

Upvotes: 8

Views: 17699

Answers (2)

Nur
Nur

Reputation: 2473

There is readFileAssets is a method in react-native-fs.

Place your file in android\app\src\main\assets.If there is no assets folder then just create it.

import fs from "react-native-fs";

fs.readFileAssets("folder/file", "base64") // 'base64' for binary 
  .then(binary => {
    // work with it
  })
  .catch(console.error)

Note: Path must be relative. If android\app\src\main\assets\folder\file then use folder\file

Upvotes: 6

Nooruddin Lakhani
Nooruddin Lakhani

Reputation: 6967

let path = RNFetchBlob.fs.asset('my-asset.jpg')

RNFetchBlob.fs.asset('bundle-assets://my-asset.jpg');

There is a method in doc assetfilenamestrings

Upvotes: 1

Related Questions