- Home /
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.
Answer by whydoidoit · Nov 05, 2013 at 06:47 AM
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 strea$$anonymous$$g 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.strea$$anonymous$$gAssetsPath like suggested on this page?
Also keep in $$anonymous$$d, 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 strea$$anonymous$$gAssetsPathAndroid = Application.strea$$anonymous$$gAssetsPath;
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 exa$$anonymous$$e the JAR and get files from it.
I use this: http://www.icsharpcode.net/opensource/sharpziplib/ for a lot of stuff.
Answer by shadyshrif · Jul 04, 2017 at 07:45 PM
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
Your answer
Follow this Question
Related Questions
How to write files to the streaming asset folder in Android at runtime 1 Answer
How to find a path to a folder in the Assets folder? 1 Answer
can't load music (wav and mp3 ) on android but the code works fine in editor and window build 0 Answers
Where can i find the Swords and Shovels Game Design Document? 3 Answers
insert my folder into an apk 0 Answers