zoukankan      html  css  js  c++  java
  • assets raw 资源 AssetManager

    assets raw 目录简介

    assets核心特性:不会被编译成二进制,支持子目录(可以分类,这是相对raw目录最大的好处),通过文件名访问,调用getAssets通过AssetManager访问
    res/raw核心特性:会被映射到【R.java】文件中,通过资源的ID访问(访问方便,这是相对assets目录最大的好处),不会被编译成二进制,不支持子目录

    assets 文件夹是存放不进行编译加工的原生文件,即该文件夹里面的文件不会像 xml、 java 文件那样会被预编译。常用来存放一些图片、音频、html、js、css等文件。
    如果你需要更灵活、自由的控制资源文件,那么【/assets】目录就是你的首选了。

    assets 特点:
    • 目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制这一点和【res/raw】目录下的文件一致
    • 目录下的文件是通过完整文件名访问的(assets目录为根目录),访问的时候需要使用AssetManager管理类。res目录下的资源文件都会被映射到【R.java】文件中,并且都是通过资源的ID访问的,如:R.raw.name,R.drawable.name,R.color.name等。这是和【res/raw】最大的区别。
    • 可以任意的建立子目录,而res目录中的资源文件是不能自行建立子目录的


    关于存放文件的大小限制问题:
    网上传言,assets和raw目录中不可以放超过1M(4M)的文件,否则会出问题。
    但是实际上,我在assets和raw放一个10M+的音频MediaPlayer播放时,都没有任何问题(可以完整播放)
    相反,我在assets和raw放一个1M-的音频用SoundPool播放时,都不能完全播放(只能播放十几秒,后面的全部丢失了)。
    所以,我觉得不是assets和raw目录对文件大小做了限制,是你读取文件的API对流的大小做了限制从而导致文件不能被全部读取。

    assets raw 目录下文件的访问示例

    对于assets,这里访问的文件"data.txt"是"bqt"目录下的文件,而"bqt"目录是以assets为根目录的。
    1. InputStream in = getResources().getAssets().open("bqt/data.txt");
    2. InputStream in2 = getResources().openRawResource(R.raw.data);
    3. AssetFileDescriptor afd = getResources().getAssets().openFd("bqt/data.txt");
    4. AssetFileDescriptor afd2 = getResources().openRawResourceFd(R.raw.data);

    AssetManager 简介【重要】

    Provides access to an application's raw asset files; see Resources for the way most applications will want to retrieve their resource data. This class presents a lower-level API that allows you to open and read raw files that have been bundled with the application as a simple stream of bytes.
    提供对应用程序的raw asset(原始资源)文件的访问;请参阅参考资料,了解大多数应用程序将要检索其资源数据的方式。 此类提供了一个较低级别的API,允许您打开并读取与应用程序绑定的、作为简单的字节流的原始文件。
    1. public final class AssetManager extends Object implements AutoCloseable

    Constants:Mode for open(String, int)
    • int  ACCESS_BUFFER:Attempt to load contents into memory, for fast small reads. 尝试将内容加载到内存中,以实现快速读取。
    • int  ACCESS_RANDOM:Read chunks, and seek forward and backward. 读取块,向前和向后寻找。
    • int  ACCESS_STREAMING:Read sequentially, with an occasional forward seek.  顺序读取,偶尔前向搜索。
    • int  ACCESS_UNKNOWN:no specific information about how data will be accessed. 没有关于如何访问数据的具体信息。

    open methods
    • final InputStream  open(String fileName)  Open an asset using ACCESS_STREAMING mode.
      • This provides access to files that have been bundled with an application as assets -- that is, files placed in to the "assets" directory. 这提供了将作为assets捆绑到应用程序的文件(即放置在“assets”目录中的文件)的访问方式。
      • String fileName: The name of the asset to open. This name can be hierarchical(分层的,按等级划分的;即:带目录). i.e., "docs/home.html".
    • final InputStream  open(String fileName, int accessMode)  Open an asset using an explicit access mode, returning an InputStream to read its contents.
    • final AssetFileDescriptor  openFd(String fileName)
    • final AssetFileDescriptor  openNonAssetFd(String fileName)
    • final AssetFileDescriptor  openNonAssetFd(int cookie, String fileName)  int cookie: Identifier(识别符) of the package to be opened.
    • final XmlResourceParser  openXmlResourceParser(String fileName)  Retrieve a parser for a compiled XML file.
    • final XmlResourceParser  openXmlResourceParser(int cookie, String fileName) Retrieve a parser for a compiled XML file.

    other methods
    • void  close()  Close this asset manager.
    • final String[]  getLocales()  Get the locales that this asset manager contains data for.
      • On SDK 21 (Android 5.0: Lollipop) and above, Locale strings are valid BCP-47 language tags and can be parsed using forLanguageTag(String). 在SDK 21及以上版本中,区域设置字符串是有效的BCP-47语言标签,可以使用forLanguageTag(String)进行解析。
      • On SDK 20 (Android 4.4W: Kitkat for watches) and below, locale strings are of the form ll_CC where ll is a two letter language code, and CC is a two letter country code. 在SDK 20及以下版本中,语言环境字符串的形式为ll_CC,其中ll是双字母语言代码,CC是两个字母的国家代码。
    • final String[]  list(String path)  Return a String array of all the assets at the given path.
      • Returns:String[] Array of strings, one for each asset. These file names are relative to 'path'. You can open the file by concatenating 'path' and a name in the returned string (via File) and passing that to open(). 
      • 返回:String []字符串数组,每个asset一个。 这些文件名与“路径”相关。 您可以通过连接“path”和返回的字符串中的名称(通过File)打开文件,并将其传递给open()。

    内部类:AssetManager.AssetInputStream
    1. public final class AssetManager.AssetInputStream extends InputStream 
    2017-7-6
  • 相关阅读:
    .net对象生命周期 一) 转载
    HTTP的版本 转载
    sql server sql语句判断是否有表备注并进行新增或修改
    sql server update触发器
    vs文件属性复制到输出目录 转载
    c#使用log4net记录日志 转载
    windows服务定时器 转载
    vs创建项目以后修改https为http
    消息队列 转载
    nmp 设置淘宝镜像
  • 原文地址:https://www.cnblogs.com/baiqiantao/p/7128548.html
Copyright © 2011-2022 走看看