java 以编程方式访问 res/raw 的内容 (Android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2803566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:55:26  来源:igfitidea点击:

Accessing contents of res/raw programmatically (Android)

javaandroid

提问by pablo.meier

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.

我正在与其他一些人一起开发适用于 Android 的应用程序,主要内容由我们的设计师指定为特定文件格式的文本文件,然后我们在应用程序中解析、处理和提供这些文件。我们目前将它们存储在res/raw.

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_fileto an array in the code specifying which files to process at startup.

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

Is there a way to access the resource ID's of res/rawprogramatically? 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/rawwith that of our array in the code.

有没有办法以res/raw编程方式访问资源 ID ?理想情况下,当应用程序启动时,我们可以调用以查看哪些文件在 中res/raw,处理所有这些文件,因此我们可以通过将 的内容res/raw与代码中数组的内容进行匹配来消除小开销 。

The most promising path I've seen is getAssetsfrom 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.

我见过的最有希望的路径是getAssets资源,这将让我叫list(String)AssetManager,但我已经遇到了麻烦得到这个工作,特别是因为你不能直接调用"res/raw"为您的文件路径(或至少,它不是招当我尝试过时,它不起作用。

回答by Padde

You could use reflection.

你可以使用反射。


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) { }

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

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

回答by over_optimistic

This answer may be a bit late, you should use assets folder instead. There you can have a folder hierarchy and read it as a file system.

这个答案可能有点晚了,您应该改用资产文件夹。在那里,您可以拥有一个文件夹层次结构并将其作为文件系统读取。

For your info the getAssetsreturns a manager for files in the "assets" folder of your Android project and not in "res/raw". "res/raw" goes into the "R" Java Package. assets.list(path)Returns an array of files in the given folder relative to your assets folder. The asset's folder is also not a traditional folder, but Android abstracts it so it "feels" like regular files. This becomes more important if you are doing JNI and want to read your assets from c/c++.

对于您的信息,getAssets返回 Android 项目“assets”文件夹中文件的管理器,而不是“res/raw”中的文件。“res/raw”进入“R”Java包。assets.list(path)返回给定文件夹中相对于您的资产文件夹的文件数组。资产的文件夹也不是传统文件夹,但 Android 对其进行了抽象,因此它“感觉”像常规文件。如果您正在使用 JNI 并希望从 c/c++ 读取您的资产,这将变得更加重要。

回答by over_optimistic

You could use

你可以用

Context.getResources().getIdentifier("filename", "raw", "packagename");

to get the appropriate identifier. however i don't understand why you implement your solution that complicated? what are the advantages for the designer? why they don't just put the files in drawable folder?

以获得适当的标识符。但是我不明白为什么你实施你的解决方案那么复杂?设计师的优势是什么?为什么他们不只是将文件放在 drawable 文件夹中?