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 /
  • Help Room /
avatar image
4
Question by Kiseki_ · Aug 04, 2016 at 10:59 AM · androidstreamingassetsapk expansion files

Using StreamingAssets in Android

What's up guys,

so I was trying to use JSON databse in my project for Android and I got stopped on a nasty problem:

To use the JSON database I need to load it. On the windows I load it like this:

json_database_items = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Resources/StreamingAssets/Items.json"));

Property that allows me to get straight to the application dirrectory is Application.dataPath.

It doesn't work on Android though, as app is packed there to .apk and should be used as a .zip archive, so I've googled a little and found several advices:

1)Use property Application.streamingAssets Well, I've put the StreamingAssets folder to Assets folder and tried this option. Didn't work.

2)To use WWW class, for opening database inside of .apk Didn't work as well for unknown reason. I put the Items.json straight to assets folder, tried to read ias a string this:

new WWW("file:java/mnt/asec/companyname.com/pkg.apk!/assets/Items.json").text;

But it didn't work for some unknown to me reason, though everything seems correct and java url should have took care about archived file.

I also tryied to use property to use property Application.streamingAssets along with WWW class, but for some another unknown reasong string returned with that property begins with "jar:file///" and this tripple slash does not seem to be neither working or correct to jar url syntaxis.

Right now I am out of options and I would like to get some help, because it does seem like desperate case here .___.

If you do know that I am wrong someway either made a mistake in code, or there is a simple solution to that task(Doesn't seem like that and I wouldn't mind any solution, even the difficult one) please, let me know.

Thx in forward, pals :)

P.S. Is there any meaning in trying to use 7zip sdk? If yes, could you please guide me a little in that dirrection?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by Jorgar · Apr 29, 2017 at 06:13 PM

Unfortunately, @Kiseki_ code didn't work for me. After long time trying to resolve it, I was able to run the json on android thanks to the code i found on git forums.

this is the code adapt to litjson plugin:

             string filePath = Application.streamingAssetsPath + "/Items.json";
             string jsonString;
             
             if (Application.platform == RuntimePlatform.Android)
             {
                 WWW reader = new WWW(filePath);
                 while (!reader.isDone) { }
     
                 jsonString = reader.text;
             }
             else
             {
                 jsonString = File.ReadAllText(filePath);
             }
     
             JsonData itemData = JsonMapper.ToObject(jsonString);


Hope this code help someone, since this problem was driving me crazy.

Comment
Add comment · Show 4 · 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 bruno-ribeiro · May 01, 2017 at 09:09 PM 0
Share

Your solution worked wonders here. Thanks!

avatar image Gabrihellbarbosa · Nov 29, 2017 at 02:37 PM 0
Share

Thanks, man! That worked for me!

avatar image marmildev · Mar 16, 2018 at 11:16 AM 0
Share

works for me :) tnx

avatar image jalemanyf · Jul 19, 2020 at 10:56 AM 0
Share

Your comment helps me a lot Jorgar! Thanks to share it ;)

avatar image
7

Answer by ramioooz · Sep 08, 2019 at 10:54 AM

NEW !!


www is obsolete now, you should use UnityWebRequest instead,

place your file (ex: jsonFile.json) in StreamingAssets folder and then use the following code, works both in Pc and mobile :

 var _path = Application.streamingAssetsPath + "/jsonFile.json";
 UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(_path);
 www.SendWebRequest();
 while (!www.isDone)
 {
 }
 String jsonString = www.downloadHandler.text;

hope this helps, thank you

Comment
Add comment · Show 2 · 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 luismoyanomedina · Aug 19, 2020 at 05:35 PM 0
Share

Hi! Just a small correction, if on the while block you use !www.isDone it won't work because that variable is set to true from the beginning. The boolean you need is !www.downloadHandler.isDone

avatar image Marc477 · Feb 24 at 02:59 AM 0
Share

CAREFUL! This does work on PC and Android. BUT it does NOT work on IOS.

avatar image
2

Answer by FMaz · Mar 18, 2018 at 12:44 PM

Alternatively, it is possible to use the Resources with a TextAsset. Just put your JSON file in a "Resources" folder (in this example: /Resources/MyJSONFolder/config.json)

 TextAsset file = Resources.Load("MyJSONFolder/config") as TextAsset;
 string content = file.toString();
 
 //Parse your JSON from the content variable   

That works well for me on Android.

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 nisDev60 · Mar 11, 2020 at 05:09 PM 0
Share

Thanks man your code really worked, just changed the .json file to a .txt file

avatar image
1

Answer by Kiseki_ · Aug 10, 2016 at 12:56 PM

Ok, I got it.

To use database(LitJSON plugin) along with unity on android you have to:

1)Put the database in the asset folder (Like: /Assets/Items.json)

2)Load the database via WWW

 WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/StreamingAssets/Items.json");

3)Write the database down in a persistent data folder

 File.WriteAllBytes(Application.persistentDataPath + "/Items.json", loadDB.bytes);)

Then you can read the database from persistent data folder

 json_itemData = JsonMapper.ToObject(File.ReadAllText(Application.persistentDataPath + "/StreamingAssets/" + "Items.json"));

That's it, it works, it is creepy, but it still does work. I am not kinda good programmer and I do not know the way to read the database straight to the script memory, but you can take a longer trip this way.

Thanks for paying attention

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 Ellapps · Feb 15, 2017 at 07:27 AM 0
Share

I tried your script. There isn't any error on the scripting but as I build it, it doesn't work. FYI, I have 4 json files. So I don't write 1 be.json (that is 1 out 4 file names in json)

here's the script that I adapt from your script above.

public void SoalBegin (string jsonName) { score = 0; nextSoal = true; filePath = System.IO.Path.Combine(Application.strea$$anonymous$$gAssetsPath, jsonName+".json"); StartCoroutine("Json"); soalData = Json$$anonymous$$apper.ToObject(jsonString); OnClick();

 }

 IEnumerator Json() {
     if (filePath.Contains("://")) {
         WWW www = new WWW(filePath);
         yield return www;
         jsonString = www.text;
     } else {
         jsonString = System.IO.File.ReadAllText(filePath);

      
     }
    WWW loadDB = new WWW("jar:file://"+Application .dataPath +"!/assets/Strea$$anonymous$$gAssets/.json");
     File.WriteAllBytes(Application.persistentDataPath + "/.json", loadDB.bytes);
     json_soalData = Json$$anonymous$$apper.ToObject (File.ReadAllText(Application.persistentDataPath +"/Strea$$anonymous$$gAssets/"+".json"));
 }

Is there anything that I missed

avatar image
1

Answer by rich-gg · Nov 07, 2019 at 11:23 AM

Use Better Streaming Assets https://assetstore.unity.com/packages/tools/input-management/better-streaming-assets-103788

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

114 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Android app dosen't load scenes from obb on install, until device restart 0 Answers

Loading JSON file from streamingAssets on android 1 Answer

Loading from streaming assets in obb fails.. but not always. 1 Answer

Download images and apply to sprite 0 Answers

Loading JSON from StreamingAssets on android(c#) 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