- Home /
File.WriteAllText() on Android (Json file)
Hello, I am trying to rewrite a json file to replace old data with new one on an Android device, on the PC it works perfectly just like this:
File.WriteAllText(Application.dataPath + "/Resources/Items.json", json);
where the "json" variable is a string containing the new information.
How can I accomplish this on an Android device? I've tried everything but nothing seems to work correctly.
string jsonPath = Application.streamingAssetsPath + "/Items.json";
File.WriteAllText(jsonPath, json);
This is the code I have for Android, but it dosent work, what should I change?
Thank you in advance.
Answer by Siggi206 · Jun 12, 2017 at 09:11 AM
We tried to use the solution from ExtinctSpecie, but writing to the file didn't work, so we added a .Close() after Creating the File and now it works. Here our method to get the File path:
public string GetPath()
{
string folderPath = (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer ? Application.persistentDataPath : Application.dataPath) + "/myDataFolder/";
string filePath = folderPath + "myFile.json";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
if (!File.Exists(filePath))
{
File.Create(filePath).Close();
File.WriteAllText(filePath, "myJsonText");
}
return filePath ;
}
Answer by ExtinctSpecie · Apr 04, 2017 at 01:38 PM
folderPath = (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer ? Application.persistentDataPath : Application.dataPath)+"/myDataFolder/";
filePath = folderPath + fileName + ".txt";
if (!Directory.Exists (folderPath)) {
Directory.CreateDirectory (folderPath);
}
File.WriteAllText(filePath, "hello world");
Thank you for the answer, but I have tried this code and some variations of it, and it dosen't work correcly, this is the code I wrote in:
string folderPath = (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer ? Application.persistentDataPath : Application.dataPath) + "/Strea$$anonymous$$gAssets/";
string filePath = folderPath + "Items.json";
string json;
json = ("DATA");
File.WriteAllText(filePath, json);
What's worng with it? I wasn't shure how to copy the folderpath part, but other than that I'm not shure what the problema could be.
On android there is no strea$$anonymous$$g assets path. The strea$$anonymous$$g assets are read-only on android and can only be read by using the WWW class as the assets are located in a folder inside the AP$$anonymous$$ file.
Just have a look at Strea$$anonymous$$g Assets.
The only location where you can store files (unless you aquire permission for external file access) is the persistentDataPath. It's a path that is specific for your app. If you want to use a subfolder you have to create that folder before you can save files there.
Alright, so in which folder should I store my json file and how do I acces it from my code?
Your answer
Follow this Question
Related Questions
StreamingAssets GIF images on android 0 Answers
Unity not saving a JSON file 2 Answers
Streaming Assets android 0 Answers
Cannot access JSON file from Streaming Assets in android build 1 Answer