The question is answered, right answer was accepted
Help Writing and reading files on android.
Hello, recently i've been working on an app in witch i'll use Json files access my sql database.
I'm programing everyhink on C#, and using LitJson Plugin for Json files.
The code i need to fix is supposed to Write a Json file, then read it, but i think i micht be doing something wrong with the file directory of android....
i'm using:
File.WriteAllText(Application.persistentDataPath + "/StreamingAssets/campoos.json", "[" + CamposData.ToString() + "]"); // To write my Json file
CamposData = JsonMapper.ToObject(File.ReadAllText(Application.persistentDataPath + "/StreamingAssets/campoos.json")); // To read my Json file
The code was supposed to display some info of the Json file on screen. however, when on PC it works, but at Android devices i won't.
What do you mean when you say it "doesn't work"? Is it the writing or the reading? If it's the reading, is it the file IO or the Json$$anonymous$$apper conversion?
If you're not sure, you can try things like breaking the code down (eg the reading can be in 2 steps, read the file and then convert), adding checks and logging, and looking to see if the file's been created.
Also, what do the logs say? And does the directory you're trying to write to exist?
I printed the persistent datapath on android, and looked for the file...couldn't find anything so i guess the writing isn't working ;/
Any idea on what to do?
How are you reading the Debug log while you phone is plugged into the GearVR?
$$anonymous$$ade a few more checks, I'm pretty sure Writing is now working
Documentation: "Note that on some platforms it is not possible to directly access the Strea$$anonymous$$gAssets folder because there is no file system access in the web platforms, and because it is compressed into the .apk file on Android. On those platforms, a url will be returned, which can be used using the WWW class."
Related to your comment as i can't answer directly, i didn't check for the existence of Strea$$anonymous$$gAssets file, a simple detail i simply forgot about ;/
Thx for the Help ;D
Answer by Bunny83 · Dec 04, 2015 at 01:31 PM
The problem is that the StreamingAssets folder doesn't exist on android. The streaming assets are packed into the JAR / APK file and you have to use
"jar:file://" + Application.dataPath + "!/assets/"
This works only with the WWW class as stated on the documentation page i've linked above. Of course you can't "write" to that "folder" since it's not a real folder. If you want to cache things in the persistentDataPath, you have to create any subfolders you like to use yourself. The persistentDataPath doesn't contain any subfolders by default.
The easiest way to create folders is to use the DirectoryInfo class.
Please don't close a question with "right answer selected" if there is no answer and of course no answer selected.
Follow this Question
Related Questions
Android json read and write 1 Answer
Problem with Json and Android ! 0 Answers
Read and Write Textfile on mobile (C# Unity ) 0 Answers
Writing to the Externally Mounted SD Card Android 1 Answer
Json doesnt work in android (unity) 0 Answers