• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Rikimaru · Aug 16, 2013 at 11:39 AM · androidfile-io

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rikimaru · Aug 16, 2013 at 02:15 PM 0
Share

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???

avatar image pitimoi · Aug 19, 2013 at 04:34 PM 0
Share

$$anonymous$$mmh, 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.

avatar image sanmn19 · Jan 13, 2014 at 06:53 PM 0
Share

Thank you for this invaluable answer.

avatar image
1

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);
 
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Gunhi · Jun 29, 2017 at 04:20 AM

Here you go: https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Liderangel · Jun 05, 2019 at 02:21 AM 0
Share

"It is not possible to access the StreamingAssets folder on WebGL and Android platforms"

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load function not working propperly? 1 Answer

Android File-io? 1 Answer

Reading files on Android, 1 Answer

Accessing Videos on Android Device 1 Answer

Why is this error happening 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges