Most visited

Recently visited

Added in API level 1

AssetManager

public final class AssetManager
extends Object implements AutoCloseable

java.lang.Object
   ↳ android.content.res.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.

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, int accessMode)

Open an asset using an explicit access mode, returning an InputStream to read its contents.

final InputStream open(String fileName)

Open an asset using ACCESS_STREAMING mode.

final AssetFileDescriptor openFd(String fileName)
final AssetFileDescriptor openNonAssetFd(String fileName)
final AssetFileDescriptor openNonAssetFd(int cookie, String fileName)
final XmlResourceParser openXmlResourceParser(int cookie, String fileName)

Retrieve a parser for a compiled XML file.

final XmlResourceParser openXmlResourceParser(String fileName)

Retrieve a parser for a compiled XML file.

Protected methods

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Inherited methods

From class java.lang.Object
From interface java.lang.AutoCloseable

Constants

ACCESS_BUFFER

Added in API level 1
int ACCESS_BUFFER

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

Constant Value: 3 (0x00000003)

ACCESS_RANDOM

Added in API level 1
int ACCESS_RANDOM

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

Constant Value: 1 (0x00000001)

ACCESS_STREAMING

Added in API level 1
int ACCESS_STREAMING

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

Constant Value: 2 (0x00000002)

ACCESS_UNKNOWN

Added in API level 1
int ACCESS_UNKNOWN

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

Constant Value: 0 (0x00000000)

Public methods

close

Added in API level 1
void close ()

Close this asset manager.

getLocales

Added in API level 1
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).

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.

Returns
String[]

list

Added in API level 1
String[] list (String path)

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

Parameters
path String: A relative path within the assets, i.e., "docs/home.html".
Returns
String[] 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:

open

Added in API level 1
InputStream open (String fileName, 
                int accessMode)

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 String: The name of the asset to open. This name can be hierarchical.
accessMode int: Desired access mode for retrieving the data.
Returns
InputStream
Throws
IOException

See also:

open

Added in API level 1
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.

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

See also:

openFd

Added in API level 1
AssetFileDescriptor openFd (String fileName)

Parameters
fileName String
Returns
AssetFileDescriptor
Throws
IOException

openNonAssetFd

Added in API level 1
AssetFileDescriptor openNonAssetFd (String fileName)

Parameters
fileName String
Returns
AssetFileDescriptor
Throws
IOException

openNonAssetFd

Added in API level 1
AssetFileDescriptor openNonAssetFd (int cookie, 
                String fileName)

Parameters
cookie int
fileName String
Returns
AssetFileDescriptor
Throws
IOException

openXmlResourceParser

Added in API level 1
XmlResourceParser openXmlResourceParser (int cookie, 
                String fileName)

Retrieve a parser for a compiled XML file.

Parameters
cookie int: Identifier of the package to be opened.
fileName String: The name of the file to retrieve.
Returns
XmlResourceParser
Throws
IOException

openXmlResourceParser

Added in API level 1
XmlResourceParser openXmlResourceParser (String fileName)

Retrieve a parser for a compiled XML file.

Parameters
fileName String: The name of the file to retrieve.
Returns
XmlResourceParser
Throws
IOException

Protected methods

finalize

Added in API level 1
void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
Throwable

Hooray!