Android File Handling

Hi there.

I have a problem with deleting created files on my sdcard. I looked everywhere, but cant find anything to solve my issue.

I created a xml file in a folder on my sdcard. this works fine. but when I switch to the next level, I want to delete that file and overwrite it with the new just created xml file. it works without any problems in the editor, but when I test this on my desire z, the old file isn’t deleted so the new file will not be created under the same name.
I also tried to delete the folder on exit where I save the file, but this also only works in the editor. Do I need another permission entry in the manifest to delete files and folders or what do I miss?

thanks for any help.

Danny

read this
http://forum.unity3d.com/threads/72819-Help-with-access-write-files-on-Android

Hi kagura.

I already inserted the android.permission.WRITE_EXTERNAL_STORAGE into my manifest. Creating files with the data is working fine.
When I first start, a new file is created onto my sdcard in the specified folder. The problem is, that I can’t delete that created file or directory.

Danny

And File.Delete(“/sdcard/some_file”); doesn’t work nor reports any exceptions?

Hi erique.

Exactly. Ill throw an exception and when deleting fails I show a label that the file wasnt deleted. I also checked with a file browser if the file is still there. And as I mentioned earlier, creating the file, directory works fine when I test it with the editor and with my htc phone. Deleting files and directories only works inside the editor. :frowning:

I now create a new different named file for every new level as a workaround. But this is not a good solution.

Danny

DannyDan, can you please file a bug on this and let me know the case number (only the number please) so I can check it out with my devices?
thanks

Hi Wozik.

I reported this and the case number is: 388291.

Thanks

Danny

Hi,

I have the same problem on my end. I’m working on an application for a Android tablet and i made the same steps as DannyDan. Force SD_Card permission is on and it’s all fine in the editor. I first check if the file exists and if it does i first delete it (…) and then Create it. Doesn’t work on Android. I’m using Application.persistentDataPath for my storage. When i press my Load button it load’s my first saved file and if i save my file it doesn’t overwrite the old one.

Can someone please post a solution for this?

Thanks in advance

Solved! I did this:

private void CreateXML(string xmlName, string data)
{
StreamWriter writer;
FileInfo t = new FileInfo(_FileLocation + “/” + xmlName);
if (!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(data);
writer.Close();
Debug.Log(“File written.”);
}

now i do:

private void CreateXML(string xmlName, string data)
{
StreamWriter writer;
FileInfo t = new FileInfo(_FileLocation + “/” + xmlName);

writer = t.CreateText();
writer.Write(data);
writer.Close();
Debug.Log(“File written.”);
}

apparently t.Exists doesn’t work.

OH, very nice. I will try it after work. It would be nice if this is the solution.

Danny

OK.

I changed my code and deleted the if statement to check if the file is already there and now its really working just fine.
Thanks for the tip, Ultimatemau.

Danny