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”);

Result:

DirectoryNotFoundException: Directory ‘jar:file:/data/app/com.your.company.game.apk!/assets/Some/Other/folder’ not found.

I have tried various other paths like:

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

Or

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

Or

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

Or

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.

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.

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

#if UNITY_EDITOR
using UnityEditor.Build;
using UnityEditor;
using System.IO;

public class BM_AndroidBuildPrepartion : IPreprocessBuild
{
    public int callbackOrder { get { return 0; } }
    public void OnPreprocessBuild(BuildTarget target, string path)
    {
        // Do the preprocessing here
        string[] fileEntries = Directory.GetFiles("Assets/Resources/Prefabs/alphabet", "*.prefab");
        System.IO.Directory.CreateDirectory("Assets/StreamingAssets/");
        using (StreamWriter sw = new StreamWriter("Assets/StreamingAssets/alphabet.txt", false))
        {
          
        foreach (string filename in fileEntries) {
                sw.WriteLine(Path.GetFileNameWithoutExtension(filename));
        }
          
        }
    }
}
#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/

  #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