Here’s my code, albeit very rough coding:
public void loadStack(AssetManager manager, String path) {
String[] list;
String thePath;
try {
list = manager.list(path);
if (list != null) {
for (int i = 0; i < list.length; i++) {
try {
if (list.length != 0) {
if (path.toString() == "") loadStack(manager, list[i]);
else {
thePath = path + "/" + list[i];
loadStack(manager, thePath);
}
}
String[] test = manager.list(list[i]);
if (test.length != 0) {
for (int j = 0; j < test.length; j++) {
byte[] assetBytes = readFromStream(list[i] + "/" + test[j]);
assetStack.push(assetBytes);
totalByteSize += assetBytes.length;
Log.d("Loading", "Stack loads assetBytes of length: " + Integer.toString(assetBytes.length));
// totalStackElementSize = assetStack.size();
}
}
// loadStack(manager, path + "/" + list[i]);
}
catch (IOException e) {
continue;
}
}
}
}
catch (IOException e1) {
return;
}
}
I’m still trying to improve the code, and all I’m lacking is telling apart what relative paths are subdirectories, and what relative paths are actual paths to files needed to load.
That way, I can rely on using a recursive method to walk through each and every subdirectories of the asset folder, and load only the ones I need.
Can anyone point me to the right direction? Or there’s something I’m missing out on the AssetManager documentation? Thanks in advance. I’m doing the best I can so far.
UPDATE:
Improved my code to this:
public void loadStack(AssetManager manager, String path, int level) {
try {
String[] list = manager.list(path);
if (list != null) {
for (int i = 0; i < list.length; i++) {
if (level >= 1) loadStack(manager, path + "/" + list[i], level + 1);
else if (level == 0) loadStack(manager, list[i], level + 1);
else {
byte[] byteBuffer = readFromStream(path);
assetStack.push(byteBuffer);
totalByteSize += byteBuffer.length;
}
}
}
}
catch (IOException e) {
Log.e("Loading", "Occurs in AssetLoad.loadStack(AssetManager, String, int), file can't be loaded: " + path);
throw new RuntimeException("Couldn't load the files correctly.");
}
}
I’m still trying to improve my code. The only problem is that it tends to read folders I haven’t added to the assets folder, or folders I never created before. Those causes logic errors, which I’m avoiding as much as possible.
This is a specific answer to a vague question, however, it works the same way.