【问题标题】:How can I get a directory listing of resources from my Android app?如何从我的 Android 应用程序中获取资源目录列表?
【发布时间】:2009-09-30 00:22:31
【问题描述】:

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

我正在使用以下方法打印资产目录树。

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: 调试/GFlash(6543): //AndroidManifest.xml 09-29 20:08:27.954: 调试/GFlash(6543): //META-INF 09-29 20:08:28.063:调试/GFlash(6543)://资产 09-29 20:08:28.233: 调试/GFlash(6543): //classes.dex 09-29 20:08:28.383: 调试/GFlash(6543): //com 09-29 20:08:28.533: 调试/GFlash(6543): //res 09-29 20:08:28.683: 调试/GFlash(6543): //resources.arsc

提前致谢!

约翰

【问题讨论】:

    标签: android


    【解决方案1】:

    呃。问题出在 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);
        }
    
    }
    

    约翰

    【讨论】:

    • 我试过了。它告诉我直到明天我才能接受自己的答案。
    • 这显示了根文件夹中的所有内容,但我实际上看不到资产文件夹中的任何文件,是否可以让它工作?
    • @schwiz:我遇到了同样的问题。解释/解决方案在这里:stackoverflow.com/questions/3631370/…
    【解决方案2】:

    要完全递归,您可以按如下方式更新方法:

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

    然后调用:

    final AssetManager mgr = applicationContext.getAssets();
    displayFiles(mgr, "",0);     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 2020-09-23
      相关资源
      最近更新 更多