- Home /
Load text file on Streaming assets in Android
On unity documents, Streaming assets are pressed to jar file.
I played my game on android, My text file on streaming assets did not be loaded.
string path = Application.streamingAssetsPath + "/" + filename;
if(Application.platform == RuntimePlatform.Android)
{
path = "jar:file://" + Application.dataPath + "!/assets/" + filename;
}
if (File.Exists(path))
{
FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader( file );
string str = null;
str = sr.ReadLine ();
sr.Close();
file.Close();
return str;
}
else
{
return null;
}
this is my code of read file.
I need your help T.T
Answer by pitimoi · Aug 16, 2013 at 01:20 PM
Hi,
I think the path Application.streamingAssetsPath + "/" + filename; is still valid on android, you don't need to use something else to access your data on android.
Anyway, try to read your file using a WWW instead of using a filestream, it should works better with unity.
Thanks for answer. I want to ask if I use www,how can I code that. www data = new www( "jar:file://" + Application.dataPath + "!/assets/" +filename); like this???
Mmmh, I don't have any project to test right now, but what I would try is :
WWW data = new WWW(Application.streamingAssetsPath+"/"+filename);
yield return data;
if(string.IsNullOrEmpty(data.error))
{
myText = data.text;
}
You should place it into a Coroutine to use WWW with Unity.
Answer by ereneld · Feb 22, 2017 at 08:31 AM
it is always better to use with path.combine
string filename = "";
Path.Combine(Application.streamingAssetsPath, filename);
Answer by uzairamirs · Apr 12, 2018 at 12:16 PM
@sanmn19 bro/sir, can you please help me? I am stuck in this.. I am trying to access streaming assets in an apk and failing for 3 days.. If you could help it would be very kind of you.
Your answer
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
How to set persistent data path to sdcard on android 0 Answers
is it possible to access the IOS file system 0 Answers
Load function not working propperly? 1 Answer
Can't perform IO in Unity Android build 0 Answers
Android File-io? 1 Answer