- Home /
Reading from .CSV works fine on windows editor, and no data on device (Android)
Hello,
I have several excel files which I've exported to .csv and reading the data with FileStream and saving to custom classes .. everything working perfeclty when I run on the editor.
string fileName = "/MyData/" + packages[i] + ".csv";
streamReader = new StreamReader(File.OpenRead(Application.dataPath + fileName ));
since I have several .csv files I have the names in a List of strings. (naming and spelling are correct since data is displayed accurately on the editor).
I did an android build, and a blank screen, data is not retrieved . However a crash might result the same, but I have no errors/null values when runing on the editor.
So .. Is it related to the path ? Application.dataPath problems on devices?
or something related to the file format?
or if anyone has a different suggestion.
Thanks for your time :)
For more updates about my search.
it was mentioned,
#if UNITY_ANDROID
path = "file:///"+ Application.persistentDataPath;
in this post: http://answers.unity3d.com/questions/854658/android-persistent-data-loading-fails.html
but still data didnt appear
Also I tried strea$$anonymous$$g assets ins$$anonymous$$d of dataPath again data didn't load
I tried .txt ins$$anonymous$$d of .csv .... still the same , data was not loaded .. so its not the path, its not the format ..
I$$anonymous$$O its something related to a permission on Adroid ..
Answer by Kiichi · May 13, 2015 at 12:20 PM
If you need to include this file to the build you have the only one way: place it to the Resources folder and use Resources.Load. You can use *.bytes extension for binary data. For ex.:
using (BinaryReader reader = new BinaryReader(new MemoryStream((Resources.Load(filename) as TextAsset).bytes)))
{
// Read your data here
}
@$$anonymous$$iichi your post helped me alot !! and many thanks however I am using a StreamReader and the actual file load is:
StreamReader streamReader = new StreamReader(new $$anonymous$$emoryStream((Resources.Load("Test") as TextAsset).bytes));
But using $$anonymous$$emoryStream with Resources was the way to solve my issue.
void ReadCSVFile(int j) { // StreamReader streamReader = new StreamReader(new $$anonymous$$emoryStream((Resources.Load("Assets/Resources/ashR3.csv") as TextAsset).bytes)); var instance = Resources.Load("Resources/ashR3.csv"); StreamReader streamReader = new StreamReader(new $$anonymous$$emoryStream((Resources.Load("Assets/Resources/ashR3.csv") as TextAsset).bytes));
Getting This Error
NullReferenceException: Object reference not set to an instance of an object readcsv.ReadCSVFile (System.Int32 j) (at Assets/readcsv.cs:26)
Answer by FWCorey · May 09, 2015 at 06:17 PM
Yes, you need to use a different path. From the documentation: //Data (this folder is read only, use Application.persistentDataPath to save data).
The docs say this is for iOS but the same limitation applies on Android IIRC. The standard data path is readonly. If you don't intend to edit the file at runtime, you are better off putting it in Resources and accessing it from there using Resources.Load(). Info in the docs is HERE
@FWCoreythanks for your answer. I know Application.dataPath is read only and I am reading only for that directory,
streamReader = new StreamReader(File.OpenRead(Application.dataPath + fileName ));
and don't have a write function.
I have placed my files in a directory in the root folder besides the Resources folder.
Already I have my images placed in my resources and loading from there.
So do you mean something like?
streamReader = new StreamReader(Resources.Load(fileName));
yep.. though IIRC the file needs to be renamed to have a .txt extension, even if it's binary.
@FWCorey I have located my files inside a folder in the root and that's dataPath, so do you now where to put them in order to read from persistentdataPath?
Also I got this error when running the app on an android device: IsolatedStorageException: Could not find a part of the path"/xxx/xxx/com.ccc.cccc/file.apk/$$anonymous$$yFolder/file.csv
many thanks
Docs for TextAsset are here: http://docs.unity3d.com/$$anonymous$$anual/class-TextAsset.html
Forgot it was UnityEngine.Object and not System.Object... The caveat with TextAsset is that it's readonly. If you want to edit the .csv at runtime you will need to generate a new text file in an accessible folder from the version in Resources then edit it from the new location.
Your answer
Follow this Question
Related Questions
How do you copy PersistentDataFolder between devices? 2 Answers
Why file creation on android is not working? 1 Answer
is it safe to save info in PlayerPrefs? 2 Answers
How to convert WWW type to FileStream? 1 Answer
File paths on Android 0 Answers