- Home /
Get file list from a directory on Android
Hello !
I looked for a while here and I can't find a solution to my problem :
I got some files in a subfolder in resources. At runtime, I want to dynamically create some list according to the files name. On editor no problem here.
But I can't find any solution on android because WWW can't access to a directory, it works only when we have the full path (and i can't do that here because it would be the opposite of what I'm trying to do).
Anyway, is this true ? I only see old post, is there a new unity solution to getFiles from directory on android ?
If not, I think that I can get the file's list before building on Android. But I don't want to launch a first time on editor, list everything in a new file and then build on android with this file included, because the list can change at each build ... So, is that possible to write code on pre processor ? I mean, when i click on File > Build & run, is it possible to launch a script that stored my files name and after build on Android with that new file / list.
I don't know if i make myself clear, tell me
Thanks for reading, hope someone already thought about that.
Answer by nasir_41 · Jan 19, 2017 at 04:33 PM
It works on on Android Platform Also. Make sure you are you are passing the path correctly.
string path= Application.persistentDataPath + "/MyFolder/";
DirectoryInfo dataDir = new DirectoryInfo (path);
try {
FileInfo[] fileinfo = dataDir.GetFiles ();
for (int i=0; i<fileinfo.Length; i++) {
string name=fileinfo [i].Name;
Debug.Log("name "+name);
}
} catch (System.Exception e) {
Debug.Log (e);
}
Oh really ? Where should I put my folder ? For now it's in the Resources folder, and i can't find it on android.
Should i use strea$$anonymous$$gassets folder ?
It didn’t work.... directory. Get file doesn’t work on android...
Answer by shadyshrif · Jul 04, 2017 at 07:39 PM
I solved the problem after I added my files in this directory inside the projec Assets/StreamingAssets/
then I read it inside the code
#if UNITY_ANDROID
string path = "jar:file://" + Application.dataPath + "!/assets/alphabet.txt";
WWW wwwfile = new WWW(path);
while (!wwwfile.isDone) { }
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "alphabet.t");
File.WriteAllBytes(filepath, wwwfile.bytes);
StreamReader wr = new StreamReader(filepath);
string line;
while ((line = wr.ReadLine()) != null)
{
//your code
}
#endif
hi i know it's a bit late but is this still working you know. Cause i'm having a lil bit of a problem on reading json file on android.
Answer by rich-gg · Nov 07, 2019 at 11:21 AM
Use Better Streaming Assets https://assetstore.unity.com/packages/tools/input-management/better-streaming-assets-103788
Answer by doublemax · Jan 19, 2017 at 10:44 AM
If the files are local this should work:
https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx
On Editor yes; it works like this :
DirectoryInfo levelDirectoryPath = new DirectoryInfo(dbPath);
FileInfo[] fileInfo = levelDirectoryPath.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo file in fileInfo)
{
// etc etc
}
But it doesn't work on Android
Your answer
Follow this Question
Related Questions
Sharing violation when deleting a directory used by a WWW object 0 Answers
Streaming Assets android 0 Answers
Loading Pictures from Android Device into game at runtime 1 Answer
Android Persistent Data loading fails 1 Answer
About www.error! 1 Answer