【问题标题】:Android - Load all files from a folderAndroid - 从文件夹中加载所有文件
【发布时间】:2014-02-08 21:09:08
【问题描述】:

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

我的问题是 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);

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

对于一个放屁按钮应用程序,这不是一个真正的问题。
但对于一种词汇应用程序而言,能够将新词添加到该文件夹​​并动态导入它们是一项关键功能。

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

如果您能(尽可能广泛地)向我解释解决此问题的方法,我将不胜感激!

【问题讨论】:

  • 我怎么可以给你发私信,Artoo Detoo? :D 我非常感谢你修正了我的德语拼写! ;)

标签: android file resources directory loading


【解决方案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

    }

【讨论】:

  • 这正是我所寻找的!非常感谢! ;)
  • @NameSpace,你是如何编码subfolderName的?我尝试在我的 SD 卡中只输入一个子文件夹的名称,但它无法识别它(只是一个名称),因此我在该部分得到一个空指针错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多