简体   繁体   English

Android存储媒体文件:使用raw /文件夹还是资产/文件夹?

[英]Android storing media files: use the raw/ folder or the assets/ folder?

Why choose one over the other? 为什么要选择一个? How are they stored on the phone? 它们如何存储在手机上?

Thanks 谢谢

With resources, there's built-in support for providing alternatives for different languages, OS versions, screen orientations, etc., as described here . 随着资源,有内置的支持,提供不同的语言,操作系统版本,屏幕方向等的替代品,如所描述这里 None of that is available with assets. 所有这些都不适用于资产。 Also, many parts of the API support the use of resource identifiers. 同样,API的许多部分都支持使用资源标识符。 Finally, the names of the resources are turned into constant field names that are checked at compile time, so there's less of an opportunity for mismatches between the code and the resources themselves. 最后,资源的名称将转换为在编译时检查的常量字段名称,因此,代码与资源本身之间不匹配的机会较小。 None of that applies to assets. 这些都不适用于资产。

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. 对于资源,您将必须声明所有可能使用的资源ID的列表,并计算该列表中的索引。 (This is kind of awkward and introduces opportunities for error if the set of resources changes in the development cycle.) You can retrieve a resource ID by name using getIdentifier , but this loses the benefits of compile-time checking. (这很尴尬,如果资源的集合在开发周期中发生变化,则会带来出错的机会。)您可以使用getIdentifier按名称检索资源ID,但这会失去编译时检查的好处。 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). 资产文件必须存在于应用程序项目的资产目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM