Android - 从文件夹加载所有文件

时间:2014-02-08 21:09:08

标签: android file resources directory loading

在我的特定情况下,我正在编写一个fartbutton-app 但我想知道如何做到这一点 - 特别是对于另一个更严肃的应用程序。

我的问题是我在res / raw目录中有三种不同的放屁声 现在,我像这样单独手动加载它们:

// soundPool is the class's attribute.
// Initialize SoundPool with all sounds:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

soundCollection[0] = soundPool.load(this, R.raw.fart1, 0);
soundCollection[1] = soundPool.load(this, R.raw.fart2, 0);
soundCollection[2] = soundPool.load(this, R.raw.fart3, 0);

我希望能够在(任意)特定文件夹中加载所有文件。
这样我就可以轻松添加更多声音,而无需手动导入。

对于fartbutton-app,这不是一个真正的问题 但是对于某种词汇应用,能够在该文件夹中添加新单词并动态导入它们是一个关键特性。

我在搜索时发现的许多建议都没有详细说明,让我无法遵循它们 大多数时候他们也是关于从SD卡加载文件。

如果你能解释我(尽可能广泛地)解决这个问题的方法,我将非常感谢你的时间!

1 个答案:

答案 0 :(得分:1)

我们走了,删除了我的旧答案,这应该是一个更直接的解决方案:

将子文件夹放在资源目录中,以下内容应迭代并打开每个资源。

    String[] file_arr = null;
    Context m_context = getActivity();

    try {
        file_arr = m_context.getAssets().list(subfolderName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    InputStream is;

    for(String filename : file_arr){
        try {
            is = m_context.getAssets().open(filename);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //..do something

    }