- Home /
How to access a text file once it's on Android Device so I can read it.
I've thoroughly read through every related question and tried everything, so I'm sure it's just me not doing it right.
I can't seem to find out the write combination of filepaths to access the text file after everything is compressed into APK.
Do I need to use Appication.persistentDataPath at all? I have no idea. I've tried everything.
if (Application.platform == RuntimePlatform.Android)
{
filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);
LoadWWW (filePath);
}
private void LoadWWW(string fileName)
{
try
{
string line;
WWW www = new WWW(filePath);
StringReader reader = new StringReader(www.text, Encoding.Default);
using (reader)
{
do
{
line = reader.ReadLine ();
if(line != null)
;//do something
}
while(line != null);
reader.Close();
}
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
I'm sure I'm doing something super obviously wrong, I'm really new to c# and unity and everything. Any help would be great.
Answer by Jeff-Kesselman · Nov 13, 2014 at 09:24 PM
in line 17 you do a read line
in line 24 you do another read line into the same variable, over-writing the first line you read
reverse the order of lines 24 and 25 (or everything that "Do Something" in line 25 represents)
"Do Something" first, THEN read the next line.
OH sorry I shouldn't have included that. But that is intentional. I erased most of the code that makes that part make sense. What it's doing is it searches the text for a line then that line tells the for loop how many iterations to go through and then "do something" is for those specific lines only. I edited that part out to prevent any confusion
In the editor it works perfectly, it reads the file and everything. It's a pathing issue and Android needing to use WWW that is causing the device either not to be able to read the file or StreamReader not working with a www.text, or something else I'm doing wrong, I don't know for sure.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
changing text using .text works in editor but not on android device 0 Answers
[Android] Open .txt file from c# 1 Answer
Adjusting UI to cellphone screen size 0 Answers