Hello Android devs.
I have researched the generic question “how to write a file on android” and not found an answer. (in one post there is a reference to a plugin, which no longer exists)
I am trying to write a file on Android. It is an XML file and I am using the mono file I/O
I have verified I can read and write just fine on PC standalone.
but on Android I think the default android manifest does not have file read/write permissions. I did find one posting regarding the android manafest (Android Plugin and permissions - Questions & Answers - Unity Discussions) but the reply didn’t have enough detail to be useful.
Below I have included the code I use to open the file for writing, and the error I get from the android adb(adb logcat -s Unity)
Here is the code I use to open/create the file
void Start () {
// Where we want to save and load to and from
string assetsPath = Application.dataPath;
_FileLocation = assetsPath.Substring(0,assetsPath.LastIndexOf('/'));
_FileName="MuzicubeRiff.xml";
}
public void OpenSaveFileForWrite()
{
FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName);
if(!t.Exists)
{
_writer = t.CreateText();
}
else
{
t.Delete();
_writer = t.CreateText();
}
}
and here is the adb logcat output
I/Unity (11522): UnauthorizedAccessException: Access to the path "/data/app/com.SmoothCurvesInteractive.MuziCube-2.apk\MuzicubeRiff.xml" is denied.
I/Unity (11522): at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
I/Unity (11522): at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
I/Unity (11522): at System.IO.FileInfo.Open (FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
I/Unity (11522): at System.IO.FileInfo.Open (FileMode mode, FileAccess access) [0x00000] in <filename unknown>:0
I/Unity (11522): at System.IO.FileInfo.CreateText () [0x00000] in <filename unknown>:0
I/Unity (11522): at (wrapper remoting-invoke-with-check) System.IO.FileInfo: CreateText ()
I/Unity (11522): at MuzicubeXML.OpenSaveFileForWrite () [0x00000] in <filena me unknown>:0
I/Unity (11522): at SaveButton.ToggleMe () [0x00000] in <filename unknown>:0
I/Unity (11522): at SaveButton.OnCustomTouch (.TouchItem aTItem) [0x0D/Unity (11522): onPause