Question by
Harvelon · Nov 03, 2021 at 04:40 PM ·
jsonfilesloading filewrite databytearray
File.ReadAllBytes() returns byte[0]
I have a simple saving and loading system in place for my exercise tracker and i am trying to save and load an image to file. Currently it saves to teh file perfectly but when i load it just returns null. Can i have some help with loading?
public void Load()
{
string json = File.ReadAllText(Application.persistentDataPath + "/Saves/save1");
SaveData loadedData = JsonUtility.FromJson<SaveData>(json);
save = loadedData;
foreach (Exercise ex in loadedData.exercises)
{
Debug.Log(Application.persistentDataPath + "/Images/" + ex.exerciseName + ".png");
ex.image.LoadImage(File.ReadAllBytes(Application.persistentDataPath + "/Images/" + ex.exerciseName + ".png"));
}
}
public void Save()
{
foreach (Exercise ex in save.exercises)
{
if (!Directory.Exists(Application.persistentDataPath + "/Images/" + ex.exerciseName + ".png"))
{
File.WriteAllBytes(Application.persistentDataPath + "/Images/" + ex.exerciseName + ".png", ex.image.EncodeToPNG());
}
}
string json = JsonUtility.ToJson(save);
File.WriteAllText(Application.persistentDataPath + "/Saves/save1", json);
Debug.Log(json);
}
Comment
Your answer
Follow this Question
Related Questions
Location for files in order to be able to load them on Android 2 Answers
How do I return a series of arrays from a SimpleJSON Object 1 Answer
[Error:] Cannot Deserialize JSON to new instances of type ' X ' 1 Answer
reading and writing files in built project,Files 0 Answers
how to open json file in editor 1 Answer