How can a create a list of all the files in the File.Assets directory?
"File.ListFiles" will not work for this directory.
I need this for my present app development.
Help.
one way is by using Inline Java BUT NOTE: See reply #2 = It is not possible to list the asset files.
The solution below is the same as using File.ListFiles with path given.
B4X:
#If JAVA
import java.io.File;
//List the files from a folder and return as stringarray
/*
Example:
Private joFiles As JavaObject = Me
'Get all files from the dirassets folder
Dim f() As String = joFiles.RunMethod("ListFiles", Array("<PATHtoFILESfolder>"))
If f = Null Then Return
For i = 0 To f.Length - 1
Log(f(i))
Next
*/
public static String[] ListFiles(String searchpath) {
File f = null;
String[] paths;
try {
f = new File(searchpath);
paths = f.list();
return paths;
} catch(Exception e){
e.printStackTrace();
return null;
}
}
#End If
Thanks for the hint = Have tested the code in release mode, also run the app from the command window. No issues encountered. Files are listed as expected.
If your .jar file is called MyJar.jar, do as follows:
B4X:
Dim arch as Archiver 'Search the forum for the Archiver library and download it
File.Copy(File.DirApp, "MyJar.jar", File.DirApp, "MyJar.zip")
arch.UnZip(File.DirApp, "MyJar.zip", File.DirApp & "\MyJarFile", "arch")
Dim fl As List = File.ListFiles(File.DirApp & "\MyJarFile\Files")
For j = 0 To fl.Size-1
Log(fl.Get(j))
Next
Make sure to delete MyJar.zip and the entire MyJarFile directory when you're done.
Explored and tested a further solution by reading the content of the Jar file - again using Inline Java.
B4X:
#If JAVA
import java.io.*;
import java.util.*;
import java.util.zip.*;
//List the content of a jar archive
//Example of getting the File Assets from a Jar - which are stored in the Files folder:
// Private joFiles As JavaObject = Me
// Dim f As List = joFiles.RunMethod("JarContent", Array("<Here FullPathToTheJarFile>"))
// If f = Null Then Return
// Log("Total number of files found in the Jar: " & f.size)
// Log("Asset Files:")
// For i = 0 To f.size - 1
// Dim s As String = f.Get(i)
// If s.StartsWith("Files") Then Log(f.Get(i))
// Next
//
public static List<String> JarContent(String searchpath) {
List<String> files = new ArrayList<String>();
try {
ZipFile zipFile = new ZipFile(searchpath);
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
while (zipEntries.hasMoreElements()) {
files.add(zipEntries.nextElement().getName());
}
return files;
} catch (IOException ioe) {
ioe.printStackTrace();
return null;
}
}