以编程方式访问res/raw的内容(Android) [英] Accessing contents of res/raw programmatically (Android)

查看:62
本文介绍了以编程方式访问res/raw的内容(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与其他一些人一起为Android开发应用程序,我们的设计师将主要内容指定为某种文件格式的文本文件,然后我们对其进行解析,处理和提供服务.我们目前将它们存储在 res/raw 中.

I'm working on an app for Android with a few other people, and the primary content is specified by our designers as text files in a certain file format, which we then parse, process, and serve in the app. We're currently storing them in res/raw.

这对设计师来说非常有用,因为当他们想要添加内容时,他们只需将文件添加到 res/raw .但是,这对于开发人员来说很烦人,因为我们开发人员需要在代码中的数组中添加 R.raw.the_new_file ,以指定启动时要处理的文件.

This makes things great for the designers because when they want to add content, they simply add a file to res/raw. This is annoying as a developer, however, since we developers then need to add R.raw.the_new_file to an array in the code specifying which files to process at startup.

是否可以通过编程方式访问 res/raw 的资源ID?理想情况下,当应用程序启动时,我们可以调用以查看哪些文件位于 res/raw 中,并对所有文件进行处理,因此我们可以通过匹配 res的内容来消除少量开销/raw 与代码中数组的值.

Is there a way to access the resource ID's of res/raw programatically? Ideally when the application starts, we could make a call to see which files are in res/raw, process all of them, so we could eliminate the small overhead with matching up the contents of res/raw with that of our array in the code.

我所见过的最有希望的路径是来自资源的 getAssets a>,这会让我叫作为文件路径(或者至少没有,我尝试过后没用.

The most promising path I've seen is getAssets from Resources, which would let me call list(String) on the AssetManager, but I've had trouble getting this to work, especially since you can't directly call "res/raw" as your filepath (or at least, it hasn't worked when I've tried.

推荐答案

您可以使用反射.


ArrayList<Integer> list = new ArrayList<Integer>();
Field[] fields = R.raw.class.getFields();
for(Field f : fields)
try {
        list.add(f.getInt(null));
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) { }

该列表之后,将所有资源ID保留在res/raw文件夹中.如果您需要资源的名称... f.getName()会返回它.

After that list holds all resource IDs in the res/raw folder. If you need the name of the resource... f.getName() returns it.

这篇关于以编程方式访问res/raw的内容(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆