Using StreamingAssets in Android
What's up guys,
so I was trying to use JSON databse in my project for Android and I got stopped on a nasty problem:
To use the JSON database I need to load it. On the windows I load it like this:
json_database_items = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Resources/StreamingAssets/Items.json"));
Property that allows me to get straight to the application dirrectory is Application.dataPath.
It doesn't work on Android though, as app is packed there to .apk and should be used as a .zip archive, so I've googled a little and found several advices:
1)Use property Application.streamingAssets Well, I've put the StreamingAssets folder to Assets folder and tried this option. Didn't work.
2)To use WWW class, for opening database inside of .apk Didn't work as well for unknown reason. I put the Items.json straight to assets folder, tried to read ias a string this:
new WWW("file:java/mnt/asec/companyname.com/pkg.apk!/assets/Items.json").text;
But it didn't work for some unknown to me reason, though everything seems correct and java url should have took care about archived file.
I also tryied to use property to use property Application.streamingAssets along with WWW class, but for some another unknown reasong string returned with that property begins with "jar:file///" and this tripple slash does not seem to be neither working or correct to jar url syntaxis.
Right now I am out of options and I would like to get some help, because it does seem like desperate case here .___.
If you do know that I am wrong someway either made a mistake in code, or there is a simple solution to that task(Doesn't seem like that and I wouldn't mind any solution, even the difficult one) please, let me know.
Thx in forward, pals :)
P.S. Is there any meaning in trying to use 7zip sdk? If yes, could you please guide me a little in that dirrection?
Answer by Jorgar · Apr 29, 2017 at 06:13 PM
Unfortunately, @Kiseki_ code didn't work for me. After long time trying to resolve it, I was able to run the json on android thanks to the code i found on git forums.
this is the code adapt to litjson plugin:
string filePath = Application.streamingAssetsPath + "/Items.json";
string jsonString;
if (Application.platform == RuntimePlatform.Android)
{
WWW reader = new WWW(filePath);
while (!reader.isDone) { }
jsonString = reader.text;
}
else
{
jsonString = File.ReadAllText(filePath);
}
JsonData itemData = JsonMapper.ToObject(jsonString);
Hope this code help someone, since this problem was driving me crazy.
Your comment helps me a lot Jorgar! Thanks to share it ;)
Answer by ramioooz · Sep 08, 2019 at 10:54 AM
NEW !!
www is obsolete now, you should use UnityWebRequest instead,
place your file (ex: jsonFile.json) in StreamingAssets folder and then use the following code, works both in Pc and mobile :
var _path = Application.streamingAssetsPath + "/jsonFile.json";
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(_path);
www.SendWebRequest();
while (!www.isDone)
{
}
String jsonString = www.downloadHandler.text;
hope this helps, thank you
Hi! Just a small correction, if on the while
block you use !www.isDone
it won't work because that variable is set to true from the beginning. The boolean you need is !www.downloadHandler.isDone
CAREFUL! This does work on PC and Android. BUT it does NOT work on IOS.
Answer by FMaz · Mar 18, 2018 at 12:44 PM
Alternatively, it is possible to use the Resources with a TextAsset. Just put your JSON file in a "Resources" folder (in this example: /Resources/MyJSONFolder/config.json)
TextAsset file = Resources.Load("MyJSONFolder/config") as TextAsset;
string content = file.toString();
//Parse your JSON from the content variable
That works well for me on Android.
Thanks man your code really worked, just changed the .json file to a .txt file
Answer by Kiseki_ · Aug 10, 2016 at 12:56 PM
Ok, I got it.
To use database(LitJSON plugin) along with unity on android you have to:
1)Put the database in the asset folder (Like: /Assets/Items.json)
2)Load the database via WWW
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/StreamingAssets/Items.json");
3)Write the database down in a persistent data folder
File.WriteAllBytes(Application.persistentDataPath + "/Items.json", loadDB.bytes);)
Then you can read the database from persistent data folder
json_itemData = JsonMapper.ToObject(File.ReadAllText(Application.persistentDataPath + "/StreamingAssets/" + "Items.json"));
That's it, it works, it is creepy, but it still does work. I am not kinda good programmer and I do not know the way to read the database straight to the script memory, but you can take a longer trip this way.
Thanks for paying attention
I tried your script. There isn't any error on the scripting but as I build it, it doesn't work. FYI, I have 4 json files. So I don't write 1 be.json (that is 1 out 4 file names in json)
here's the script that I adapt from your script above.
public void SoalBegin (string jsonName) { score = 0; nextSoal = true; filePath = System.IO.Path.Combine(Application.strea$$anonymous$$gAssetsPath, jsonName+".json"); StartCoroutine("Json"); soalData = Json$$anonymous$$apper.ToObject(jsonString); OnClick();
}
IEnumerator Json() {
if (filePath.Contains("://")) {
WWW www = new WWW(filePath);
yield return www;
jsonString = www.text;
} else {
jsonString = System.IO.File.ReadAllText(filePath);
}
WWW loadDB = new WWW("jar:file://"+Application .dataPath +"!/assets/Strea$$anonymous$$gAssets/.json");
File.WriteAllBytes(Application.persistentDataPath + "/.json", loadDB.bytes);
json_soalData = Json$$anonymous$$apper.ToObject (File.ReadAllText(Application.persistentDataPath +"/Strea$$anonymous$$gAssets/"+".json"));
}
Is there anything that I missed
Answer by rich-gg · Nov 07, 2019 at 11:23 AM
Use Better Streaming Assets https://assetstore.unity.com/packages/tools/input-management/better-streaming-assets-103788