Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Bypp · Jul 09, 2014 at 05:23 PM · c#androidtextfile

[Android] Open .txt file from c#

Hi !

I'd like to open a file in a text editor (whichever one the user chooses/has). None of that Filestream to read line by line and write stuff, just bring me to the editor and show me the file.

I've tried this :

 string dataPath = Application.streamingAssetsPath+ "/somedoc/" + fileName;
 Application.OpenURL(dataPath);
 

The code above doesn't do anything (doesn't trigger anything in the debug log or do anything). Debugging to check if System.IO.File.Exists(l_DataPath) returns false. Thing is, I'm using pretty much the same path for iOS and PC and it works.

Other things I've tried :

  • Changing the path to Application.dataPath and Application.persistentDataPath doesn't work more. The Exists() check still returns false, but this time around a popup comes up asking me with what editor I want to open the file. Whatever I choose, once I open up the editor, it just tells me he's unable to load the file or something.

  • Adding "file://" or "file:///" before the path. Didn't change a thing.

I'm kind of out of ideas here ! Anyone knows how to achieve this ?

BONUS QUESTION : Should I finally be able to make the above work, is there a way to open the text file in readonly mode ?

Thanks for anyone taking the time to help me out !

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

1 Reply

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

Answer by Bypp · Jul 10, 2014 at 01:16 PM

Found a solution that works ! Here's the thing, the streaming assets path, on Android, returns a path that can only be read by a WWW object. So I simply read it with a WWW object then recreated the file in my persistent data path. Added a check to make sure the file doesn't already exist before creating it. Also, make sure you create the directory in case it doesn't exist, else you'll get an error. Note that this solution is probably not optimal if you have large files that are regularly accessed.

 string realPath = Application.persistentDataPath + "/PATH/" + fileName;

 if (!System.IO.File.Exists(realPath))
 {
     if (!System.IO.Directory.Exists(Application.persistentDataPath + "/PATH/"))
     {
         System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/PATH/");
     }

     WWW reader = new WWW(Application.streamingAssetsPath + "/PATH/" + realPath);
     while ( ! reader.isDone) {}

     System.IO.File.WriteAllBytes(realPath, reader.bytes);
 }

 Application.OpenURL(realPath);

If anyone has anything to add to this answer, feel free !

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Loading TextAsset returns null 1 Answer

Open APK inside a game isn't working! 0 Answers

How do you create a folder in c#? 3 Answers

Save and load prefabs instantiated at runtime 0 Answers

changing text using .text works in editor but not on android device 0 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