- Home /
How to refer to internal json file on Android?
In my Assets folder I have a subfolder named Text which contains a json file containing all dialogue in my game. I access this file as follows:
string loadedConversations = File.ReadAllText(Application.dataPath + "/Text/conversations.json");
conversations = JsonUtility.FromJson<ComlinkConversations>(loadedConversations).conversations;
This works great in the editor and on standalone PC builds - however, on Android it returns this error message:
04-22 17:33:32.549 25870-25884/(Package Name) E/Unity: IsolatedStorageException: Could not find a part of the path "/data/app/(Package Name)-2/base.apk/Text/conversations.json".
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x001f9] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:290
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
Is there something else I have to do to be able to load in internal json files at runtime? I searched around previous questions and came across a suggestion to enable "write access to external (sd card)" in the build settings but this did not make a difference.
If it makes a difference, I don't need to actually write to this file at any time, only read from it.
Answer by Bunny83 · Apr 23, 2019 at 01:05 AM
You have two issues here. First of all an Android build consists of a single APK file which contains all content. So there is no direct way to access any individual files since they don't exist as individual files. An APK file is just a ZIP archive.
The second issue you have is that just because you copy a file into your project it isn't included in your build game. Only "used" / referenced assets are included. The root are scene files that are included in the build settings. Scenes can contain gameobjects with scripts which may reference other assets including prefabs.
Besides normal assets you can also create a Resources folder inside your assets folder. Assets inside a Resources folder are always included in a build and you can access such assets using the Resources class using a path that is relative to the Resources folder.
Json files should be recognised by Unity as TextAssets. To access the content of a TextAsset you would use either the text property to access the content as string or the bytes property to get a generic byte array. For more information on TextAssets see the manual page
The easiest way to use a json file that should be part of your built game is to just declare a TextAsset variable and drag the file onto the variable.
public TextAsset jsonData;
Now you can simply do:
conversations = JsonUtility.FromJson<ComlinkConversations>(jsonData.text).conversations;
Your answer
Follow this Question
Related Questions
Search for files on android 2 Answers
Print to a file for Android using Javascript 1 Answer
File.WriteAllText() on Android (Json file) 2 Answers
OBB. How I can access files in OBB? 0 Answers
Unity Android JSON Read / Write 0 Answers