问题描述
我的工作与其他几个人对于Android的应用程序,是由我们的设计师在一个特定文件格式的文本文件,我们再分析,处理和服务的应用程序中指定的主要内容。目前,我们正在将它们存储在 RES /生
。
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 /生
。这是烦人作为一个开发者,但是,由于我们的开发人员则需要添加 R.raw.the_new_file
在code指定数组要处理的文件在启动时
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.
有没有办法编程访问资源的ID的 RES /生?在应用程序启动理想的时候,我们可以打个电话,看看哪些文件在
RES /生
,处理所有的人,这样我们就可以消除小的开销匹配起来内容 RES /生
与我们在code数组中。
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.
我见过的最有希望的路径是<$c$c>getAssets$c$c>从资源,这将让我叫<$c$c>list(String)$c$c>在AssetManager,但我有麻烦这项工作,特别是因为你不能直接调用RES /生
为您的文件路径(或至少,它没有工作当我已经试过。
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) { }
在该列表保存在res /原始文件夹中的所有资源的ID。如果你需要的资源的名称... 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.
这篇关于Accesing资源的内容/原始编程(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!