简体   繁体   English

我应该在什么时候使用资产而不是 Android 中的原始资源?

[英]When should I use the assets as opposed to raw resources in Android?

I'm in the mid of my Android studies, and I just covered the Assets and Raw resources.我正处于 Android 研究的中期,我刚刚介绍了 Assets 和 Raw 资源。 I'm trying to understand the reason for using Raw resources vs. Assets.我试图了解使用原始资源与资产的原因。

  1. They both provide with an uncompiled resource input stream.它们都提供未编译的资源输入流。

  2. It seems that Assets provide much more flexibility and functionality than Raw resources.资产似乎比原始资源提供了更多的灵活性和功能。

    a.一种。 You can create folder structures under assets but not under raw您可以在assets下创建文件夹结构,但不能在raw下创建

    b.You can list all resources dynamically in the assets folder but not in the raw folder.您可以在assets文件夹中动态列出所有资源,但不能在raw文件夹中列出。

So, why would I use Raw resources in Android?那么,为什么我要在 Android 中使用 Raw 资源?

The main differences between the raw folder and the assets folder. raw文件夹和assets文件夹之间的主要区别。

  • Since raw is a subfolder of Resources (res), Android will automatically generate an ID for any file located inside it.由于 raw 是 Resources (res) 的子文件夹,Android 会自动为位于其中的任何文件生成ID This ID is then stored in the R class that will act as a reference to a file, meaning it can be easily accessed from other Android classes and methods and even in Android XML files.这个ID然后存储在R class ,作为对文件的引用,这意味着它可以很容易地从其他 Android 类和方法甚至在 Android XML 文件中访问。 Using the automatically generated ID is the fastest way to have access to a file in Android.使用自动生成的 ID 是在 Android 中访问文件的最快方式。

  • The assets folder is an “appendix” directory. assets文件夹是一个“附录”目录。 The R class does not generate IDs for the files placed there, which is less compatible with some Android classes and methods. R 类不会为放置在那里的文件生成 ID ,这与某些 Android 类和方法的兼容性较差。 File access in the assets folder is slower since you will need to get a handle to it based on a String . assets文件夹中的文件访问速度较慢,因为您需要根据 String 获取它的句柄 However some operations are more easily done by placing files in this folder, like copying a database file to the system's memory.但是,通过将文件放在此文件夹中可以更轻松地完成某些操作,例如将数据库文件复制到系统内存中。 There's no (easy) way to create an Android XML reference to files inside the Assets folder.没有(简单的)方法可以创建对 Assets 文件夹内文件的 Android XML 引用。

From the Android documentation , the raw/ directory is used for:Android 文档中raw/目录用于:

Arbitrary files to save in their raw form.以原始形式保存的任意文件。 To open these resources with a raw InputStream , call Resources.openRawResource() with the resource ID, which is R.raw.filename .要使用原始InputStream打开这些资源,请使用资源 ID(即R.raw.filename 调用Resources.openRawResource()

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/) .但是,如果您需要访问原始文件名和文件层次结构,您可以考虑在assets/目录(而不是 res/raw/)中保存一些资源。 Files in assets/ are not given a resource ID, so you can read them only using AssetManager . assets/中的文件没有资源 ID,因此您只能使用AssetManager读取它们。


In one line, the files in the raw/ directory are not compiled by the platform , are assigned a resource ID and cannot be grouped into sub-folders whereas if you want the otherwise use the assets/ directory.在一行中, raw/目录中的文件不是由平台编译的被分配了一个资源 ID并且不能被分组到子文件夹中,而如果你想要否则使用assets/目录。

Adding to the answers given above...添加到上面给出的答案...

/res/strings,/res/layout,/res/xml files etc all gets compiled into binary format. /res/strings、/res/layout、/res/xml 文件等都被编译成二进制格式。 But if you place files, including XML files, in the /res/raw/ directory instead, they don't get compiled into binary format.但是,如果您将文件(包括 XML 文件)放在 /res/raw/ 目录中,则它们不会被编译为二进制格式。

One big advantage of using assets over raw resources is the file:///android_asset/ Uri prefix.This is useful for loading an asset into a WebView.使用资产而不是原始资源的一大优势是file:///android_asset/ Uri前缀。这对于将资产加载到 WebView 很有用。 For example, for accessing an asset located in assets/foo/index.html within your project, you can call loadUrl("file:///android_asset/foo/index.html") loading that HTML into the WebView.例如,要访问位于项目中 assets/foo/index.html 的资产,您可以调用loadUrl("file:///android_asset/foo/index.html")将该 HTML 加载到 WebView 中。

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

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