• 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
3
Question by and_duch · Dec 15, 2015 at 09:51 PM · android

How do I Read/Save File in Android

I'm developing an App for Android and I need to Read and save files in .txt. I'm using C# and the library System.IO to read and write my .txt files. When I Start my App I need to Read some .txt files that are locate in a folder. So to give the Path to this folder I'm using:

string path = Application.PersistentDataPath + "/file.txt"; StreamReader file = new StreamReader(path);

But when i Build the project and put in my cellphone, the android can't find my .txt files. I tried to put a text object to see where the variable path is, the return that i get is:

/storage/sdcard0/Android/data/com.andrew.myApp/files/teste.txt

I've tried to change in playerprefs the write acess and install location. tried every combination and the App couldn't find my .txt file.

If anyone can help it would be great

Comment
Add comment · Show 2
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 Arshia001 · Dec 16, 2015 at 06:09 PM 0
Share

What do you mean by "doesn't find"? What exception do you get? Can you write files?

avatar image cubecoders · Dec 16, 2015 at 10:16 PM 0
Share

As an aside, use Path.Combine(Application.PersistentDataPath, "file.txt") - It's neater and works better across different platforms.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ArshakKroyan · Dec 17, 2015 at 02:42 PM

Hi @and_duch. Are you sure that you have selected "PlayerSettings->OtherSettings->WriteAccess to External(SDCard)?

Also try Application.dataPath instead of Application.persistentDataPath. I don't remember which one is OK for Android.

Have you got any error which will be helpful ?

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 Tyrfing12 · Mar 26, 2016 at 07:41 PM

Hi @and_duch, Try using ES File Explorer, and go to sdcard/android/data/com.andrew.myApp/files/teste.txt. It work for me :)

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
5

Answer by BytesCrafter · Aug 04, 2016 at 03:09 PM

Hope this help, I am also working on a project that needs a method something like this. :D

public class TestScript : MonoBehaviour { public void SaveEncodedFile() { string path = Application.persistentDataPath;

     File.WriteAllText(path + "/Testest.txt", testst.text);
 }

 public void LoadEncodedFile()
 {
     string path = Application.persistentDataPath;
     tests.text = File.ReadAllText(path + "/" + "TempName" + ".txt");
 }

}

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 OgnjenSelver · Feb 11 at 09:28 AM

One way is to use: 1. public TextAsset YourFile; 2.Drag it in inspector Printing it on UI would look something like: (i have in my script "public Text txt;" drag and drop your UI element and also include using UnityEngine.UI;) txt.text=YourFile.text;

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

invoke keyboard on android? 1 Answer

Unity IAP Issue - Android 1 Answer

Skinned mesh increasing vertex count. 0 Answers

GUI.Button press (not click) 3 Answers

Skin mesh model merge (iPhone and android) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges