- Home /
About Android can't load json file in Application.persistentDataPath
Hi all,
I put json file in Application.persistentDataPath,then It's can load and work on PC.
But It can't work in Android ,I'm stuck for a long time.
please help me , thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class QuizCon : MonoBehaviour
{
public RoundData[] allRoundData;
private string gameDataFileName = "quiz.json";
private void Start()
{
DontDestroyOnLoad(gameObject);
LoadGameData();
}
public RoundData GetCurrectRoundData()
{
return allRoundData[0];
}
private void LoadGameData()
{
string filePath = Path.Combine(Application.persistentDataPath, gameDataFileName);
if(File.Exists(filePath))
{
string dataJson = File.ReadAllText(filePath);
GameData loadedData = JsonUtility.FromJson<GameData>(dataJson);
allRoundData = loadedData.allRoundData;
}
else
{
Debug.LogError("Cannot load game data!");
}
}
Answer by Bunny83 · Aug 16, 2019 at 09:47 AM
Well you said:
I put json file in Application.persistentDataPath
Did you actually put the file there on your android device? Usually you don't have access to that folder from outside your own application. So the only app that is able to store anything in that folder is your own app. The persistent data folder is always empty when you install an app. You can not ship files already in that folder. The folder is for persistent storate on the device, it's not a data-shipping folder. This folder is created by the Android OS for your app. When you install an app, everything that belongs to your app is within the APK file. Android does not install other files for you.
If you want to ship a json file with your app, you need to actually include it in your game files. A common way is to place the file in the streaming assets folder. You would still try to read the file from the persistent folder first. If there is no file, you would use w UnityWebRequest to retrieve the content from the streaming assets and then store it yourselt in the persistent folder.
If the json data is not meant to be changed by your application but only read you can directly load it from the streaming assets through the UnityWebRequest. Note: The streaming assets folder for most build targets is an actual folder within your data folder that is shipped as actual folder to the user. However on Android as mentioned everything is packed into the APK file (which essentially is a zip file). So you can not use the usual System.IO.File stuff. The UnityWebRequest and the WWW class can use a special URN which is returned by Application.streamingAssetsPath
oh ! I got the wrong . I put the file in Application.persistentDataPath on Windows.
So , my android device don't have the file.
At first , I put the json file in folder of Strea$$anonymous$$gAssets ,then I use Application.strea$$anonymous$$gAssetsPath ,it's still work . When I build the apk and test on android, it can't work.
I'm confusing now . Application.persistentDataPath can't build in apk?
Hi , I solve the problem. I put the file in Strea$$anonymous$$gAssets , and use UnityWebRequest to get the file ,thank you for your suggestion.
Hi, @MOJAJA ! Did you use the code from below to access Strea$$anonymous$$gAssets folder with UnityWebRequest? var _path = Application.strea$$anonymous$$gAssetsPath + "/jsonFile.json"; UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(_path); www.SendWebRequest(); while (!www.isDone) { } String jsonString = www.downloadHandler.text;
If yes, there's one more question: where should I put this code? Should I create an empty gameobject in my main menu(because the very first scene to load in my game is Main Menu) and attach a script to it, where in Awake() function I put the code from above?
Your answer
Follow this Question
Related Questions
Problems when saving and loading with FileStream 1 Answer
Application.persistentDataPath returning null on certain Android devices 2 Answers
Save via XML or JSON for mobile game 2 Answers
Saves on android, could not find file 0 Answers
How to send an Email with the details entered in a json file? 0 Answers