我知道res目录中的文件可以从R.class访问,而资产的行为就像一个文件系统,但我想知道,在一般情况下,什么时候最好使用一个和另一个。 谁能帮我了解一下资产和资产的真正区别?
当前回答
如果您需要在Java代码中的某个地方引用它们,那么您宁愿将文件放到“res”目录中。
res文件夹中的所有文件都将被索引到R文件中,这使得加载它们更快(也更容易!)
其他回答
我知道这是旧的,但只是为了让它更清楚,在官方的android文档中有一个解释:
从http://developer.android.com/tools/projects/index.html
资产/
这是空的。您可以使用它来存储原始资产文件。保存在这里的文件将按原样编译为.apk文件,并保留原始文件名。您可以以与使用uri的典型文件系统相同的方式导航此目录,并使用AssetManager将文件作为字节流读取。例如,这是纹理和游戏数据的好位置。
res - raw
对于任意原始资产文件。在这里保存资产文件而不是在assets/目录中保存资产文件的唯一不同之处在于您访问它们的方式。这些文件由aapt处理,应用程序必须使用R类中的资源标识符引用这些文件。例如,这是媒体的好地方,如MP3或Ogg文件。
资产提供了一种在应用程序中包含任意文件(如文本、xml、字体、音乐和视频)的方法。如果你试图将这些文件作为“资源”,Android会将它们处理到它的资源系统中,你将无法获得原始数据。如果您希望不加改动地访问数据,Assets是一种方法。
有了资源,就有了内置的支持,可以为不同的语言、OS版本、屏幕方向等提供替代方案,如下所述。这些资产都不具备。此外,API的许多部分支持使用资源标识符。最后,资源的名称被转换为常量字段名,在编译时进行检查,这样代码和资源本身之间不匹配的机会就会减少。这些都不适用于资产。
So why have an assets folder at all? If you want to compute the asset you want to use at run time, it's pretty easy. With resources, you would have to declare a list of all the resource IDs that might be used and compute an index into the the list. (This is kind of awkward and introduces opportunities for error if the set of resources changes in the development cycle.) (EDIT: you can retrieve a resource ID by name using getIdentifier, but this loses the benefits of compile-time checking.) Assets can also be organized into a folder hierarchy, which is not supported by resources. It's a different way of managing data. Although resources cover most of the cases, assets have their occasional use.
One other difference: 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). [EDIT: With Android's new Gradle-based build system (used with Android Studio), this is no longer true. Asset directories for library projects are packaged into the .aar files, so assets defined in library projects are merged into application projects (so they do not have to be present in the application's /assets directory if they are in a referenced library).]
EDIT: Yet another difference arises if you want to package a custom font with your app. There are API calls to create a Typeface from a font file stored in the file system or in your app's assets/ directory. But there is no API to create a Typeface from a font file stored in the res/ directory (or from an InputStream, which would allow use of the res/ directory). [NOTE: With Android O (now available in alpha preview) you will be able to include custom fonts as resources. See the description here of this long-overdue feature. However, as long as your minimum API level is 25 or less, you'll have to stick with packaging custom fonts as assets rather than as resources.]
以下是一些要点:
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访问任何文件系统中的任何文件一样。这个目录可以存放游戏细节、字典等等。
推荐文章
- 安卓视图模型与视图模型
- Android的clipToPadding属性做什么?
- 导航抽屉:如何在启动时设置所选项目?
- 在XML中“图像上缺少contentDescription属性”
- 在Android SQLite中处理日期的最佳方法
- 读取Android APK的包名
- Android-Facebook应用程序的键散列
- 登出时,清除活动历史堆栈,防止“返回”按钮打开已登录的活动
- 如何改变标题的活动在安卓?
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout