LB2
LB2

Reputation: 1

Saving a txt file to the Oculus Go Internal Storage

I'm building an Oculus Go app with Unity but I can't figure out how to save a txt file to the Oculus Storage. I have read everything I've found online about it, including using solutions proposed on this website here and here.

I'm trying to create a text file that records which button from a toggle group was selected by the user.

When I build the same thing for PC the code works but I can't find the files inside the Oculus Go. I have edited the OVR android manifest and have a very simple script made from following a couple of tutorials.

Any help would be appreciated, thank you!

using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using System.IO;
public class SubmitandWriteButtonOVR : MonoBehaviour

{
    //GameObject
    public ToggleGroup toggleGroup;

    public void onClick()
    {
        string selectedLabel = toggleGroup.ActiveToggles()
            .First().GetComponentsInChildren<Text>()
            .First(t => t.name == "Label").text;

        //Path of the file
        string path = Application.persistentDataPath + "/Answers.txt";

        //Create file if it doesn't exist
        if (!File.Exists(path))
        {
            File.WriteAllText(path, "Answers");
        }

        //Content of the file Get the label in activated toggles
        string content = "Selected Answers: \n" + System.DateTime.Now + "\n" + selectedLabel;

        //Add some text
        File.AppendAllText(path, content);

    }
}

Upvotes: 0

Views: 1221

Answers (1)

nrs
nrs

Reputation: 371

There are several things that could be the error here:

1) Try to set your rootpath like this

rootPath = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android", StringComparison.Ordinal));

2) Even if your rootpath is set correctly the file might not show up. Try putting Answers.txt manually onto your GO, write something into Answers.txt from inside your App, restart your go and check again in Explorer if it wrote to your file. I read that it has to do with the Android file system not realizing that this file has been created/updated, so it doesn't show.

3) Make sure you have write permissions on externalSD via PlayerSettings

Here is my solution for a StreamWriter on Android:

private void WriteLogToFile()
    {
        Debug.Log("Starting to Write Logfile");
        string path = Path.Combine(rootPath, "Logs.txt");

        StreamWriter writer = new StreamWriter(path, true);
        writer.WriteLine("TEST");

        writer.Close();
        Debug.Log("Logging Done");
    }

Try if that, in combination with all of the tips above, helps you to reach your goal and let me know.

Upvotes: 0

Related Questions