列出来自 Android App returns 的包中的文件 null

List files in a package from Android App returns null

我开发了一个 Android 应用程序,主要是 activity 我想使用以下代码行列出特定包中的所有文件:

String[] files = new File("src/com/android/app/").list();

它returns空!?有什么不对 ? 谢谢

JavadocsFile class:

User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:

An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "\\" for a Microsoft Windows UNC pathname, and
A sequence of zero or more string names.

看来您缺少前缀字符串。换句话说,路径名不是相对的,而是绝对的。

我解决了问题,而所有classes都是Dalvik虚拟机编译的,必须使用DexFile class Dex File documentation.

public List<String> getClasses(Context context, String package) {
    List<String> classes = new LinkedList<String>();
    try {
        DexFile dexFile = new DexFile(context.getPackageCodePath());
            for (Enumeration<String> iter = dexFile.entries(); iter.hasMoreElements();) {
                String currentClass = iter.nextElement();
                    if (currentClass.contains(package)) {
                            classes.add(currentClass);
                    }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    return classes;
}