Does DirectoryInfo GetFiles work on Android ?

I need to list up all png files in a sub folder of the assets folder in my exported android project.

I have verified that the sub folder is in the assets folder of my exported android project. It has some .png files in it.

However, I keep getting the error: DirectoryNotFoundException .

Does DirectoryInfo GetFiles work on android ?

Sample Code:

string streamingAssetsPathAndroid = "jar:file://" + Application.dataPath + "!/assets/";

string subFolderPath = "Some/Other/" + "folder";

string fullpath = streamingAssetsPathAndroid + subFolderPath;

DirectoryInfo dirInfo = new DirectoryInfo(fullpath);

FileInfo[] fileInfo = dir.GetFiles("*.png");


DirectoryNotFoundException: Directory 'jar:file:/data/app/!/assets/Some/Other/folder' not found.

I have tried various other paths like:

string streamingAssetsPathAndroid = "jar:file://" + Application.dataPath + "!/assets/";


string streamingAssetsPathAndroid = "file://" + Application.dataPath + "!/assets/";


string streamingAssetsPathAndroid = "Application.dataPath + "!/assets/";


string streamingAssetsPathAndroid = "assets/";

and then combining it with the sub folder path. But I get a DirectoryNotFoundException error every time.

Please let me know if there is a proper/recommended way to do this.

avatar image
Best Answer

Answer by whydoidoit 

Well resources just in Assets aren't going to be in your build, they get packed up and all sorts, and then only if they are used. Assets in the Streaming Assets folder will be copied into the jar for your build.

The sub folders and pngs are in my streaming assets folder in unity. When it gets exported as a google android project, they are in the android project's assets folder.

Is there some (alternative) way to list up all .png files in my sub folder ?

Hmm, is there a reason you don't use Application.streamingAssetsPath like suggested on this page?

Also keep in mind, like mentioned on the doc page, on android the files are not in a folder, they are within a jar file. So GetFiles won't work on this path since it's not an actual path. If you want to "browse" your folder you need a zip-library to open the jar manually.

GetFiles works perfectly well on real folders as long as you have file permission.

I actually have tried using: string streamingAssetsPathAndroid = Application.streamingAssetsPath;

Which pretty much results in the same path string and the same folder not found error for me.

I may have to consider that it may not be possible to use Directory.GetFiles on a unity game exported to google android project...

Bunny's right you can't scan the directory because it is in a ZIP file. You can use a ZIP library to examine the JAR and get files from it.

Show more comments
avatar image

Answer by shadyshrif 

To solve this problem I created pre-compilation script which will run before the compilation; this script will list the names of the files you want to access later in text file

  2. using UnityEditor.Build;
  3. using UnityEditor;
  4. using System.IO;
  5. public class BM_AndroidBuildPrepartion : IPreprocessBuild
  6. {
  7. public int callbackOrder { get { return 0; } }
  8. public void OnPreprocessBuild(BuildTarget target, string path)
  9. {
  10. // Do the preprocessing here
  11. string[] fileEntries = Directory.GetFiles("Assets/Resources/Prefabs/alphabet", "*.prefab");
  12. System.IO.Directory.CreateDirectory("Assets/StreamingAssets/");
  13. using (StreamWriter sw = new StreamWriter("Assets/StreamingAssets/alphabet.txt", false))
  14. {
  15. foreach (string filename in fileEntries) {
  16. sw.WriteLine(Path.GetFileNameWithoutExtension(filename));
  17. }
  18. }
  19. }
  20. }
  21. #endif

then I read the text file and you can access the files in same way but you have to put them inside your project in Assets/StreamingAssets/

  2. string path = "jar:file://" + Application.dataPath + "!/assets/alphabet.txt";
  3. WWW wwwfile = new WWW(path);
  4. while (!wwwfile.isDone) { }
  5. var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "alphabet.t");
  6. File.WriteAllBytes(filepath, wwwfile.bytes);
  7. StreamReader wr = new StreamReader(filepath);
  8. string line;
  9. while ((line = wr.ReadLine()) != null)
  10. {
  11. //your code
  12. }
  13. #endif

ref :

+ Recent posts