public final class

AssetManager

extends Object
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()
Called before the object's memory is reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int ACCESS_BUFFER

Since: 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

Since: 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

Since: 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

Since: 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 ()

Since: API Level 1

Close this asset manager.

public final String[] getLocales ()

Since: API Level 1

Get the locales that this asset manager contains data for.

public final String[] list (String path)

Since: 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)

Since: 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)

Since: 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)

Since: API Level 1

Throws
IOException

public final AssetFileDescriptor openNonAssetFd (String fileName)

Since: API Level 1

Throws
IOException

public final AssetFileDescriptor openNonAssetFd (int cookie, String fileName)

Since: API Level 1

Throws
IOException

public final XmlResourceParser openXmlResourceParser (String fileName)

Since: 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)

Since: 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 ()

Since: API Level 1

Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.

Throws
Throwable