- Home /
Android - Straight answer for accessing data via Application.dataPath
I've started porting my game to Android and the first thing I tried (like any good programmer) was to build and run. :) I noticed that none of the xml files (that set initial values) were loaded. I've basically traced it down to what looks like the xml serializer is not finding the files. I don't know how to know if and where my data files are getting saved when my game deploys.
I did a bunch of searching around and found all kinds of answers (some out of date, some solving other issues). I just want a clear solution for accessing data on android.
My code pre-android is such:
public static void setDefaultFileLocation() {
fileLocation = Application.dataPath + "\\Data";
Debug.Log("fileLocation : " + fileLocation);
}
public static void SaveXML<T>(T data, string fileName) {
if (string.IsNullOrEmpty(fileLocation))
setDefaultFileLocation();
string _data = SerializeObject<T>(data);
StreamWriter writer;
FileInfo t = new FileInfo(fileLocation + "\\" + fileName + ".xml");
if (!t.Exists) {
writer = t.CreateText();
}
else {
t.Delete();
writer = t.CreateText();
}
writer.Write(_data);
writer.Close();
Debug.Log("XML File saved : " + fileName);
}
public static T LoadXML<T>(string fileName) {
if (string.IsNullOrEmpty(fileLocation))
setDefaultFileLocation();
if (!File.Exists(fileLocation + "\\" + fileName + ".xml"))
return default(T);
StreamReader r = File.OpenText(fileLocation + "\\" + fileName + ".xml");
string _info = r.ReadToEnd();
r.Close();
string _data = _info;
Debug.Log("XML File loaded : " + fileName);
return (T)DeserializeObject<T>(_data);
}
for reference, i save all of my files under the "Data" folder in the path location.
So, how can I modify this to get android to find, access, and mod the files?
What type of data are you trying to read/write? If you want to safely read/write custom data at run-time, you're only option is to use Application.persistentDataPath. In my experience, trying to read/write to any other directory on Android has result in security exceptions at run-time.
It's custom data classes (level name, score, powerup locations/types, level stats, ect). Ok, how do I make sure my data gets put in that directory pre-deployment? It doesn't seem to match application.datapath when working locally
Are these files that are setup at build time, such as config files, or are these files that are being generated at run-time, such as save files? I think that by default, the Application.persistentDataPath is on the user's device is empty by default.
If you want to read a read-only file, I recommend placing it in a Resources directory, and using Resources.Load()
to load it at run-time. If you want to read/write a file, you'll need to store it in Application.persistentDataPath.
These are config files (they are not generated at runtime)... they get modified though. Seems like i may need to copy data over to the Application.persistentDataPath on first launch. I've never used Resources.Load() to access an X$$anonymous$$L file. Is that possible...?
I'm 99% sure that data in Resources is read-only. If you want to modify it, read it from Resources, and then create a new, writable copy in Application.persistentDataPath.
Answer by hiddenspring81 · May 29, 2013 at 08:11 PM
What type of data are you trying to read/write? If you want to safely read/write custom data at run-time, you're only option is to use Application.persistentDataPath. In my experience, trying to read/write to any other directory on Android has result in security exceptions at run-time.
It should be noted that Application.persistentDataPath is empty on the user's device by default (and I don't think that there is a way to override this behavior).
I am not sure if I understand. I have a csv file with some texts to handle translation en/fr so I put my csv in the Asset folder and access it with Application.dataPath (doesn't work on Android). How am I supposed to use Application.persistentDataPath ? I can save a file at Application.persistentDataPath, but my file already exists the first time my game starts, so I need to access it the first time if I want to save it somewhere else, so back to square one.
EDIT : Resources.Load() worked for me. After reading this whole page my conclusion is Resources.Load() is the alternative to Application.dataPath for mobiles, then Application.persistentDataPath is here if we need to back up files after the first play.
Your answer
Follow this Question
Related Questions
How to read json file in android game? 1 Answer
Path for Assets/Textures folder File in Android 2 Answers
build file with one game and two game can access it 0 Answers
Accessing custom resources on Android 1 Answer
Android StreamingAssets File Access 6 Answers