1

I'm trying to populate my ROOM database with a simple JSON that I have inside the raw folder, this is my code

fun getInstance(context: Context): CarDatabase {
    if(instance == null) {
        instance =  Room.databaseBuilder (
            context,
            CarDatabase::class.java, "cars_table"
        ).createFromAsset("app/res/raw/cars_sample.json").build()
    }
    return instance as CarDatabase
}

And I get this

Caused by: java.io.FileNotFoundException: app/res/raw/cars_sample.json

AmrDeveloper
  • 2,559
  • 1
  • 16
  • 25
SNM
  • 3,896
  • 4
  • 22
  • 60

2 Answers2

0

Unless your module has src/main/assets/app/res/raw/cars_sample.json, that is not a valid path within assets/ to your JSON. And, since Android cannot find that JSON, my guess is your module does not have that asset.

From the looks of what you are using, my guess is that you are trying to populate your database from a raw resource. Raw resources are similar to assets, but they are not assets.

You will need to create an assets/ directory for your module and move your JSON there.

CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367
  • Hi, thanks for the answer, but I think I cannot move the json to assets because my requirement is to populate my database from the raw resource folder, is there any way of doing it ? – SNM Sep 18 '21 at 18:51
  • @SNM: You could try using `openRawResource()` on `Resources` to get an `InputStream` and use that [with `createFromInputStream()` on `RoomDatabase.Builder`](https://developer.android.com/reference/androidx/room/RoomDatabase.Builder#createFromInputStream(java.util.concurrent.Callable%3Cjava.io.InputStream%3E)). I have not tried that though. – CommonsWare Sep 18 '21 at 19:01
-2

Since Android Studio uses the new Gradle-based build system, you should be putting assets/ inside of the source sets (e.g., src/main/assets/).

In a typical Android Studio project, you will have an app/ module, with a main/ sourceset (app/src/main/ off of the project root), and so your primary assets would go in app/src/main/assets/. However:

If you need assets specific to a build type, such as debug versus release, you can create sourcesets for those roles (e.g,. app/src/release/assets/)

Your product flavors can also have sourcesets with assets (e.g., app/src/googleplay/assets/)

Your instrumentation tests can have an androidTest sourceset with custom assets (e.g., app/src/androidTest/assets/), though be sure to ask the InstrumentationRegistry for getContext(), not getTargetContext(), to access those assets

Also, a quick reminder: assets are read-only at runtime. Use internal storage, external storage, or the Storage Access Framework for read/write content.

The raw folder must be inside the res folder, otherwise it won't work.

To create a raw folder:

  1. Right-click the res folder.
  2. Choose New.
  3. Choose Android Resource Directory.
  4. Name the directory raw.
  5. In the Resource Types Section add raw.
  6. Click ok.
Hossein
  • 492
  • 2
  • 9
  • I have already the json inside the raw folder below resources, is not working – SNM Sep 18 '21 at 18:49
  • Take a look at https://stackoverflow.com/questions/18302603/where-to-place-the-assets-folder-in-android-studio/18302624#18302624 – Hossein Sep 18 '21 at 18:52