我知道res目录中的文件可以从R.class访问,而资产的行为就像一个文件系统,但我想知道,在一般情况下,什么时候最好使用一个和另一个。 谁能帮我了解一下资产和资产的真正区别?


当前回答

如果您需要在Java代码中的某个地方引用它们,那么您宁愿将文件放到“res”目录中。

res文件夹中的所有文件都将被索引到R文件中,这使得加载它们更快(也更容易!)

其他回答

以下是一些要点:

Raw files Must have names that are valid Java identifiers , whereas files in Assets Have no location and name restrictions. In other words they can be grouped in whatever directories we wish Raw files Are easy to refer to from Java as well as from xml (i.e you can refer a file in raw from manifest or other xml file). Saving asset files here instead of in the assets/ directory only differs in the way that you access them as documented here http://developer.android.com/tools/projects/index.html. Resources defined in a library project are automatically imported to application projects that depend on the library. For assets, that doesn't happen; asset files must be present in the assets directory of the application project(s) The assets directory is more like a filesystem provides more freedom to put any file you would like in there. You then can access each of the files in that system as you would when accessing any file in any file system through Java . like Game data files , Fonts , textures etc. Unlike Resources, Assets can can be organized into subfolders in the assets directory However, the only thing you can do with an asset is get an input stream. Thus, it does not make much sense to store your strings or bitmaps in assets, but you can store custom-format data such as input correction dictionaries or game maps. Raw can give you a compile time check by generating your R.java file however If you want to copy your database to private directory you can use Assets which are made for streaming.

结论

Android API包括一个非常舒适的资源框架 还针对各种移动应用程序的最典型用例进行了优化。 您应该掌握参考资料,并尽可能地使用它们。 然而,如果您的特殊情况需要更大的灵活性,Assets 有没有给你一个更低级的API来组织和 以更高的自由度处理资源。

使用文件系统之类的资产来转储任何类型的文件。并使用分辨率来存储它所做的,布局,图像,值。

两者都很相似。两者之间真正的主要区别是,在res目录中,每个文件都有一个预编译的ID,可以通过R.id轻松访问。(res id)。这是有用的快速和轻松地访问图像,声音,图标…

assets目录更像一个文件系统,提供了更大的自由,可以在其中放置任何您想要的文件。然后,您可以访问该系统中的每个文件,就像通过Java访问任何文件系统中的任何文件一样。这个目录可以存放游戏细节、字典等等。

如果您需要在Java代码中的某个地方引用它们,那么您宁愿将文件放到“res”目录中。

res文件夹中的所有文件都将被索引到R文件中,这使得加载它们更快(也更容易!)

Ted Hopp很好地回答了这个问题。我一直在使用res/raw为我的opengl纹理和着色器文件。我正在考虑将它们移动到一个资产目录,以提供一个分层组织。

这个帖子说服了我不要这么做。首先,因为我喜欢使用唯一的资源id。其次,使用InputStream/openRawResource或BitmapFactory读取文件非常简单。第三,在便携式图书馆中使用它是非常有用的。