[Android] Open .txt file from c#

Hi !

I’d like to open a file in a text editor (whichever one the user chooses/has). None of that Filestream to read line by line and write stuff, just bring me to the editor and show me the file.

I’ve tried this :

string dataPath = Application.streamingAssetsPath+ "/somedoc/" + fileName;
Application.OpenURL(dataPath);

The code above doesn’t do anything (doesn’t trigger anything in the debug log or do anything). Debugging to check if System.IO.File.Exists(l_DataPath) returns false. Thing is, I’m using pretty much the same path for iOS and PC and it works.

Other things I’ve tried :

  • Changing the path to Application.dataPath and Application.persistentDataPath doesn’t work more. The Exists() check still returns false, but this time around a popup comes up asking me with what editor I want to open the file. Whatever I choose, once I open up the editor, it just tells me he’s unable to load the file or something.

  • Adding “file://” or “file:///” before the path. Didn’t change a thing.

I’m kind of out of ideas here ! Anyone knows how to achieve this ?

BONUS QUESTION : Should I finally be able to make the above work, is there a way to open the text file in readonly mode ?

Thanks for anyone taking the time to help me out !

Found a solution that works ! Here’s the thing, the streaming assets path, on Android, returns a path that can only be read by a WWW object. So I simply read it with a WWW object then recreated the file in my persistent data path. Added a check to make sure the file doesn’t already exist before creating it. Also, make sure you create the directory in case it doesn’t exist, else you’ll get an error. Note that this solution is probably not optimal if you have large files that are regularly accessed.

string realPath = Application.persistentDataPath + "/PATH/" + fileName;

if (!System.IO.File.Exists(realPath))
{
    if (!System.IO.Directory.Exists(Application.persistentDataPath + "/PATH/"))
    {
        System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/PATH/");
    }

    WWW reader = new WWW(Application.streamingAssetsPath + "/PATH/" + realPath);
    while ( ! reader.isDone) {}

    System.IO.File.WriteAllBytes(realPath, reader.bytes);
}

Application.OpenURL(realPath);

If anyone has anything to add to this answer, feel free !