Android APIs
public final class

AssetManager

extends Object
implements AutoCloseable
java.lang.Object
   ↳ android.content.res.AssetManager

Class Overview

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.

Summary

Nested Classes
class AssetManager.AssetInputStream  
Constants
int ACCESS_BUFFER Mode for open(String, int): Attempt to load contents into memory, for fast small reads.
int ACCESS_RANDOM Mode for open(String, int): Read chunks, and seek forward and backward.
int ACCESS_STREAMING Mode for open(String, int): Read sequentially, with an occasional forward seek.
int ACCESS_UNKNOWN Mode for open(String, int): no specific information about how data will be accessed.
Public Methods
void close()
Close this asset manager.
final String[] getLocales()
Get the locales that this asset manager contains data for.
final String[] list(String path)
Return a String array of all the assets at the given path.
final InputStream open(String fileName)
Open an asset using ACCESS_STREAMING mode.
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)
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.
Protected Methods
void finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.AutoCloseable

Constants

public static final int ACCESS_BUFFER

Added in API level 1

Mode for open(String, int): Attempt to load contents into memory, for fast small reads.

Constant Value: 3 (0x00000003)

public static final int ACCESS_RANDOM

Added in API level 1

Mode for open(String, int): Read chunks, and seek forward and backward.

Constant Value: 1 (0x00000001)

public static final int ACCESS_STREAMING

Added in API level 1

Mode for open(String, int): Read sequentially, with an occasional forward seek.

Constant Value: 2 (0x00000002)

public static final int ACCESS_UNKNOWN

Added in API level 1

Mode for open(String, int): no specific information about how data will be accessed.

Constant Value: 0 (0x00000000)

Public Methods

public void close ()

Added in API level 1

Close this asset manager.

public final String[] getLocales ()

Added in API level 1

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).

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.

public final String[] list (String path)

Added in API level 1

Return a String array of all the assets at the given path.

Parameters
path A relative path within the assets, i.e., "docs/home.html".
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().
Throws
IOException
See Also

public final InputStream open (String fileName)

Added in API level 1

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.

Parameters
fileName The name of the asset to open. This name can be hierarchical.
Throws
IOException

public final InputStream open (String fileName, int accessMode)

Added in API level 1

Open an asset using an explicit access mode, returning an InputStream to read its contents. This provides access to files that have been bundled with an application as assets -- that is, files placed in to the "assets" directory.

Parameters
fileName The name of the asset to open. This name can be hierarchical.
accessMode Desired access mode for retrieving the data.
Throws
IOException

public final AssetFileDescriptor openFd (String fileName)

Added in API level 1

Throws
IOException

public final AssetFileDescriptor openNonAssetFd (String fileName)

Added in API level 1

Throws
IOException

public final AssetFileDescriptor openNonAssetFd (int cookie, String fileName)

Added in API level 1

Throws
IOException

public final XmlResourceParser openXmlResourceParser (String fileName)

Added in API level 1

Retrieve a parser for a compiled XML file.

Parameters
fileName The name of the file to retrieve.
Throws
IOException

public final XmlResourceParser openXmlResourceParser (int cookie, String fileName)

Added in API level 1

Retrieve a parser for a compiled XML file.

Parameters
cookie Identifier of the package to be opened.
fileName The name of the file to retrieve.
Throws
IOException

Protected Methods

protected void finalize ()

Added in API level 1

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

Throws
Throwable