[Solved] Reading text file on Android

As a preface, I have already looked through all other mentionings of people with this problem, but none of their solutions seem to work.

In my project, I’m attempting to load levels stored as text files with the .map extension inside the StreamingAssets folder. On PC, this works flawlessly, no problems there, but no matter what I try I cannot get it to function on Android.

What I’m currently using (while faulty) to load files is:

public void LoadScene(){
		if(Application.isMobilePlatform){
			using(StreamReader sr = new StreamReader(Application.persistentDataPath + "/StreamingAssets/Levels/" + levelName)){
				gridX = int.Parse(sr.ReadLine ());
				gridY = int.Parse(sr.ReadLine ());

				level = new int[gridX, gridY];
				string[] yList = new string[gridY];

			for(int i = 0; i < yList.Length; i++){
					yList *= sr.ReadLine();*
  •  		}*
    
  •  		for(int y = 0; y < gridY; y++) {	*
    
  •  			for(int x = 0; x < gridX; x++){*
    
  •  			string[] xList = yList[y].Split(',');*
    
  •  				level[x, y] = int.Parse (xList[x]);*
    
  •  				//print (level[x, y]);*
    
  •  			}*
    
  •  		}*
    
  •  	}*
    
  •  }else{*
    
  •  	if(Application.dataPath + "/StreamingAssets/Levels/" + levelName != null){*
    
  •  		using(StreamReader sr = new StreamReader(Application.dataPath + "/StreamingAssets/Levels/" + levelName)){*
    
  •  			gridX = int.Parse(sr.ReadLine());*
    
  •  			gridY = int.Parse(sr.ReadLine());*
    
  •  			level = new int[gridX, gridY];*
    
  •  			string[] yList = new string[gridY];*
    
  •  			for(int i = 0; i < yList.Length; i++) {*
    

_ yList = sr.ReadLine();_
* }*

* for(int y = 0; y<gridY; y++) { *
* for (int x = 0; x<gridX; x++) {*
* string[] xList = yList [y].Split(‘,’);*
* level[x, y] = int.Parse(xList[x]);*
* //print (level[x, y]);*
* }*
* }*
* }*
* }else{*
* print (“Level not found”);*
* }*
* }*
The levels are stored in (on PC) streamingassets/Levels/

The path you are looking for is Application.streamingAssetsPath, for all platforms.

It’s really only coincidence that Application.dataPath + “/StreamingAssets” works on PC, because the streaming assets folder is directly inside the data directory. That doesn’t work on Android because the data path points to the apk, whereas the streaming assets folder is several levels deep inside the apk.

Application.persistentDataPath is also an incorrect location to look for streaming assets on Android, because it points to an entirely different directory outside the apk where your app can write files (not possible inside the apk).

That said, there are other considerations for reading raw files from inside an Android apk, and I’m not sure off the top of my head if using StreamReader for a path inside the apk would work. If you don’t have a particular reason for them to be in streaming assets, I would suggest putting them in the resources folder and use something like:

TextAsset level = Resources.Load<TextAsset>("Levels/" + levelName);
if(level != null)
{
    using(StreamReader sr = new StreamReader(new MemoryStream(level.bytes)))
    {
        ...
    }
}

You can also use webrequest, in conjunction with Application.streamingAssetsPath, as illustrated here Unity - Scripting API: Application.streamingAssetsPath