Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 funki · Apr 02, 2017 at 10:07 AM · androidjsonfilestreamingassetswrite data

File.WriteAllText() on Android (Json file)

Hello, I am trying to rewrite a json file to replace old data with new one on an Android device, on the PC it works perfectly just like this:

 File.WriteAllText(Application.dataPath + "/Resources/Items.json", json);

where the "json" variable is a string containing the new information.

How can I accomplish this on an Android device? I've tried everything but nothing seems to work correctly.

 string jsonPath = Application.streamingAssetsPath + "/Items.json";
 File.WriteAllText(jsonPath, json);

This is the code I have for Android, but it dosent work, what should I change?

Thank you in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Siggi206 · Jun 12, 2017 at 09:11 AM

We tried to use the solution from ExtinctSpecie, but writing to the file didn't work, so we added a .Close() after Creating the File and now it works. Here our method to get the File path:

    public string GetPath()
     {
         string folderPath = (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer ? Application.persistentDataPath : Application.dataPath) + "/myDataFolder/";
         string filePath = folderPath + "myFile.json";
         if (!Directory.Exists(folderPath))
         {
             Directory.CreateDirectory(folderPath); 
         }
         if (!File.Exists(filePath)) 
         {
             File.Create(filePath).Close();
             File.WriteAllText(filePath, "myJsonText");
         }
         return  filePath ;
     }




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
1

Answer by ExtinctSpecie · Apr 04, 2017 at 01:38 PM

   folderPath = (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer ? Application.persistentDataPath : Application.dataPath)+"/myDataFolder/";
  
  
  filePath = folderPath  + fileName + ".txt";
  
  if (!Directory.Exists (folderPath)) {
                  Directory.CreateDirectory (folderPath);    
              }
  
              File.WriteAllText(filePath, "hello world");   
Comment
Add comment · Show 6 · 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 funki · Apr 09, 2017 at 12:05 AM 0
Share

Thank you for the answer, but I have tried this code and some variations of it, and it dosen't work correcly, this is the code I wrote in:

 string folderPath = (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer ? Application.persistentDataPath : Application.dataPath) + "/Strea$$anonymous$$gAssets/";
 string filePath = folderPath + "Items.json";
 string json;
 json = ("DATA");
 File.WriteAllText(filePath, json);

What's worng with it? I wasn't shure how to copy the folderpath part, but other than that I'm not shure what the problema could be.

avatar image Bunny83 funki · Apr 09, 2017 at 12:40 AM 0
Share

On android there is no strea$$anonymous$$g assets path. The strea$$anonymous$$g assets are read-only on android and can only be read by using the WWW class as the assets are located in a folder inside the AP$$anonymous$$ file.

Just have a look at Strea$$anonymous$$g Assets.

The only location where you can store files (unless you aquire permission for external file access) is the persistentDataPath. It's a path that is specific for your app. If you want to use a subfolder you have to create that folder before you can save files there.

avatar image funki Bunny83 · Apr 09, 2017 at 11:19 PM 0
Share

Alright, so in which folder should I store my json file and how do I acces it from my code?

Show more comments
Show more comments

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

6 People are following this question.

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

Related Questions

How can I save data to a text/json file and read it back and then assign the values back to the variables ? 2 Answers

StreamingAssets GIF images on android 0 Answers

Unity not saving a JSON file 2 Answers

Streaming Assets android 0 Answers

Cannot access JSON file from Streaming Assets in android build 1 Answer


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