android - 如何从我的 Android 应用程序中获取资源的目录列表?

标签 android

我的 Android 应用程序在 Assets 目录中有一些文件,我想在启动时通过列出目录中的文件并打开每个文件来打开这些文件。我正在尝试使用 AssetManager 来执行此操作,但它似乎并不像我期望的那样。我的示例代码如下。这是正确的方法还是有更好的方法?

我正在使用以下方法打印出 Assets 目录树。

void displayFiles (AssetManager mgr, String path) {
    try {
        String list[] = mgr.list(path);
        if (list != null)
            for (int i=0; i<list.length; ++i)
                {
                    Log.v("Assets:", path +"/"+ list[i]);
                    displayFiles(mgr, path + list[i]);
                }
    } catch (IOException e) {
        Log.v("List error:", "can't list" + path);
    }

} 

在我的 Activity 的 onCreate 方法中,我执行以下操作:

final AssetManager mgr = getAssets();    
displayFiles(mgr, "/assets"); 
displayFiles(mgr, "./assets"); 
displayFiles(mgr, "/");
displayFiles(mgr, "./");

这给了我以下输出

09-29 20:08:27.843: DEBUG/GFlash(6543): //AndroidManifest.xml 
09-29 20:08:27.954: DEBUG/GFlash(6543): //META-INF
09-29 20:08:28.063: DEBUG/GFlash(6543): //assets
09-29 20:08:28.233: DEBUG/GFlash(6543): //classes.dex 
09-29 20:08:28.383: DEBUG/GFlash(6543): //com
09-29 20:08:28.533: DEBUG/GFlash(6543): //res
09-29 20:08:28.683: DEBUG/GFlash(6543): //resources.arsc

提前致谢!

约翰

最佳答案

呃。问题出在 displayFiles 方法中,它缺少目录和文件名之间的分隔符“/”。对不起,如果我浪费了任何人的时间。下面是 displayFiles 的更正版本。

void displayFiles (AssetManager mgr, String path) {
    try {
        String list[] = mgr.list(path);
        if (list != null)
            for (int i=0; i<list.length; ++i)
                {
                    Log.v("Assets:", path +"/"+ list[i]);
                    displayFiles(mgr, path + "/" + list[i]);
                }
    } catch (IOException e) {
        Log.v("List error:", "can't list" + path);
    }

}

约翰

关于android - 如何从我的 Android 应用程序中获取资源的目录列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1495585/

相关文章:

android - 如何从另一个 Intent 中获取信息?

android - 如何在非全屏横向模式下为切口/缺口区域着色

android - 在 MediaBrowserServiceCompat 中从 Room 加载项目

android - 如何在 Flutter 应用中播放 .mp3 文件?

android - 在 Android 上使用框架创建动画启动画面

Android 过滤 ListView 用户输入

android - 从加速度计中去除重力...文档代码

java - 发送带有附件的电子邮件,发送一个空白文件

android - 使用 Delphi 以编程方式检查 SD 卡是否可用

android - 移植 Windows 线程以在 Android 操作系统上运行