1

I need to read a Json file to load data into memory.

The project has the following structure of modules:

|_ app
|_ data
   |_ datasource
   |_ theory.json       
|_ domain

I tried to save the json file into data module, and then in the datasource class I tried to read the file to be parsed by GSON library.

So I tried into a Kotlin class of the data module:

val fileInputStream = FileInputStream("theory.json")
val fileInputStream = FileInputStream("/theory.json")
val fileInputStream = FileInputStream("../theory.json")

And always I'm getting the this error:

theory.json: open failed: ENOENT (No such file or directory)

But I put the file in the assets folder of package app, and then I do:

|_ app
   |_assets
     |_ theory.json      
|_ data
   |_ datasource 
|_ domain

val file = context?.assets?.open("theory.json")

Once I get the file I'm able to read the file and parse it with the Gson Library, so it works perfect.

The problem here is that to make the datasource work , I need to create the file into de app module, then start to pass it via params to datasource.

Is there any way to make it work "isolated" from the app module, and make it work in the data module?

Shudy
  • 7,806
  • 19
  • 63
  • 98
  • 1
    I don't think it's possible https://stackoverflow.com/questions/27441831/android-accessing-assets-form-library-project/27636288 – noev May 09 '20 at 17:46
  • Actually try this https://stackoverflow.com/a/22254488/1419353 – noev May 09 '20 at 17:47
  • Thanks, I'll take look to the second link to see if I can do something. If not, I'll leave it as I have now. Thanks! – Shudy May 09 '20 at 18:41
  • before answering in can you tell me what kind of project is this? Because on a project to project basis we have a different structures to store data to be easily read from. – silentsudo May 12 '20 at 15:37
  • @Shudy Did you find any solution for it? – Mohamed Nageh Jul 02 '20 at 16:17

0 Answers0