File IO problem with android

Hi all,

I have a file which is essentially a text file but with a different extension. e.g. “filename.xyz”.
What I would like to do is to save this file on the device, preferably in external storage where the user could access it.

I have tried using an approach where I copy the file from Assets to my desired location:

string filePath = Application.persistentDataPath;
System.IO.File.Copy ("Assets/Resources/testFile.xyz", APP_PATH + "/Sample_Files/testFile.xyz");

I have also tried using a different approach by saving the file as a string then trying to re-output it as the textfile with my desired extension:

textFile = (TextAsset)Resources.Load("Beech_Rd", typeof(TextAsset));
print (textFile.text);
System.IO.File.WriteAllText (APP_PATH + "/Sample_Files/TestFileSucess.xyz", textFile.text);

Both code segments work for PC but not for Android. I’m not sure why this is, but if anyone could point me in the right direction it would be much appreciated.

You’re using paths for the Editor on a PC, you need to be using paths for a build and those are different on each platform. The Application namespace has properties to handle this for you - Application.dataPath and Application.persistentDataPath, from memory. Check those out in the documentation.

Sorry, I ought to clarify that accessing the file via “Application.persistentDataPath” works (given I manually place the file in the right place) and that the struggle is with copying a file using scripts to this location on the Android device for future use.

Well, your example shows that you’re copying from “Assets/…”, which is a directory that does not exist on your device.

Also, have you made sure that the /Sample_Files/ sub-directory exists on the device?

Also, knowing the error you’re getting would help. Is it a null reference or an exception or…?

If it’s not too difficult to do, why not just “create” the file in code and save it to the persistentDataPath?

  1. Yeah, I thought that that was a valid operation. Not sure what to do now.
  2. Yep /Sample_Files/ is a valid and working directory
  3. Not getting an error per se, but I know the script has failed to do what I want when the target directory is empty.

This did occur to me (if I’m interpretting what you are saying correctly) and I did test this on simple strings (less than 100 char) but I would have to put all the lines onto one line and save it as a string. The barrier is that there are 30,000 lines of text in my file which rendered MonoDevelop completely unresponsive when I attempted to edit after copying the “mega line” of text in.

Anything in the Assets folder in your project gets stored as a platform-specific resource in your build, not as the source file you chucked in there. Outside of the Editor you don’t have access to that.

If you need to be able to access stuff dynamically you’ll need to look up Resources folders and see how they work. Alternatively, if you have something in your scene you’ll be able to reference it as an object and do with it as you will.