public final class AssetManager extends Object implements AutoCloseable
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.Modifier and Type | Class and Description |
---|---|
class |
AssetManager.AssetInputStream |
Modifier and Type | Field and Description |
---|---|
static int |
ACCESS_BUFFER
Mode for
open(String, int) : Attempt to load contents into
memory, for fast small reads. |
static int |
ACCESS_RANDOM
Mode for
open(String, int) : Read chunks, and seek forward and
backward. |
static int |
ACCESS_STREAMING
Mode for
open(String, int) : Read sequentially, with an
occasional forward seek. |
static int |
ACCESS_UNKNOWN
Mode for
open(String, int) : no specific information about how
data will be accessed. |
Constructor and Description |
---|
AssetManager()
Create a new AssetManager containing only the basic system assets.
|
Modifier and Type | Method and Description |
---|---|
int |
addAssetPath(String path)
Add an additional set of assets to the asset manager.
|
int |
addAssetPathAsSharedLibrary(String path)
Add an application assets to the asset manager and loading it as shared library.
|
int[] |
addAssetPaths(String[] paths)
Add multiple sets of assets to the asset manager at once.
|
int |
addOverlayPath(String idmapPath)
Add a set of assets to overlay an already added set of assets.
|
int |
addOverlayPathNative(String idmapPath)
See addOverlayPath.
|
void |
close()
Close this asset manager.
|
protected void |
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
static String |
getAssetAllocations() |
SparseArray<String> |
getAssignedPackageIdentifiers() |
String |
getCookieName(int cookie) |
static int |
getGlobalAssetCount() |
static int |
getGlobalAssetManagerCount() |
String[] |
getLocales()
Get the locales that this asset manager contains data for.
|
String[] |
getNonSystemLocales()
Same as getLocales(), except that locales that are only provided by the system (i.e. those
present in framework-res.apk or its overlays) will not be listed.
|
Configuration[] |
getSizeConfigurations() |
static AssetManager |
getSystem()
Return a global shared asset manager that provides access to only
system assets (no application assets).
|
boolean |
isUpToDate()
Determine whether the state in this asset manager is up-to-date with
the files on the filesystem.
|
String[] |
list(String path)
Return a String array of all the assets at the given path.
|
InputStream |
open(String fileName)
Open an asset using ACCESS_STREAMING mode.
|
InputStream |
open(String fileName,
int accessMode)
Open an asset using an explicit access mode, returning an InputStream to
read its contents.
|
AssetFileDescriptor |
openFd(String fileName) |
InputStream |
openNonAsset(int cookie,
String fileName)
Open a non-asset in a specified package.
|
InputStream |
openNonAsset(int cookie,
String fileName,
int accessMode)
Open a non-asset in a specified package.
|
InputStream |
openNonAsset(String fileName)
Open a non-asset file as an asset using ACCESS_STREAMING mode.
|
InputStream |
openNonAsset(String fileName,
int accessMode)
Open a non-asset file as an asset using a specific access mode.
|
AssetFileDescriptor |
openNonAssetFd(int cookie,
String fileName) |
AssetFileDescriptor |
openNonAssetFd(String fileName) |
XmlResourceParser |
openXmlResourceParser(int cookie,
String fileName)
Retrieve a parser for a compiled XML file.
|
XmlResourceParser |
openXmlResourceParser(String fileName)
Retrieve a parser for a compiled XML file.
|
void |
setConfiguration(int mcc,
int mnc,
String locale,
int orientation,
int touchscreen,
int density,
int keyboard,
int keyboardHidden,
int navigation,
int screenWidth,
int screenHeight,
int smallestScreenWidthDp,
int screenWidthDp,
int screenHeightDp,
int screenLayout,
int uiMode,
int majorVersion)
Change the configuation used when retrieving resources.
|
public static final int ACCESS_UNKNOWN
open(String, int)
: no specific information about how
data will be accessed.public static final int ACCESS_RANDOM
open(String, int)
: Read chunks, and seek forward and
backward.public static final int ACCESS_STREAMING
open(String, int)
: Read sequentially, with an
occasional forward seek.public static final int ACCESS_BUFFER
open(String, int)
: Attempt to load contents into
memory, for fast small reads.public AssetManager()
Resources.getAssets()
. Not for
use by applications.
public static AssetManager getSystem()
public void close()
close
in interface AutoCloseable
public final InputStream open(String fileName) throws IOException
fileName
- The name of the asset to open. This name can be
hierarchical.IOException
open(String, int)
,
list(java.lang.String)
public final InputStream open(String fileName, int accessMode) throws IOException
fileName
- The name of the asset to open. This name can be
hierarchical.accessMode
- Desired access mode for retrieving the data.IOException
ACCESS_UNKNOWN
,
ACCESS_STREAMING
,
ACCESS_RANDOM
,
ACCESS_BUFFER
,
open(String)
,
list(java.lang.String)
public final AssetFileDescriptor openFd(String fileName) throws IOException
IOException
public final String[] list(String path) throws IOException
path
- A relative path within the assets, i.e., "docs/home.html".IOException
open(java.lang.String)
public final InputStream openNonAsset(String fileName) throws IOException
IOException
open(String)
public final InputStream openNonAsset(String fileName, int accessMode) throws IOException
IOException
open(String, int)
public final InputStream openNonAsset(int cookie, String fileName) throws IOException
cookie
- Identifier of the package to be opened.fileName
- Name of the asset to retrieve.IOException
public final InputStream openNonAsset(int cookie, String fileName, int accessMode) throws IOException
cookie
- Identifier of the package to be opened.fileName
- Name of the asset to retrieve.accessMode
- Desired access mode for retrieving the data.IOException
public final AssetFileDescriptor openNonAssetFd(String fileName) throws IOException
IOException
public final AssetFileDescriptor openNonAssetFd(int cookie, String fileName) throws IOException
IOException
public final XmlResourceParser openXmlResourceParser(String fileName) throws IOException
fileName
- The name of the file to retrieve.IOException
public final XmlResourceParser openXmlResourceParser(int cookie, String fileName) throws IOException
cookie
- Identifier of the package to be opened.fileName
- The name of the file to retrieve.IOException
protected void finalize() throws Throwable
Object
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.
public final int addAssetPath(String path)
public final int addAssetPathAsSharedLibrary(String path)
public final int addOverlayPath(String idmapPath)
public final int addOverlayPathNative(String idmapPath)
public final int[] addAssetPaths(String[] paths)
addAssetPath(String)
for more information. Returns array of
cookies for each added asset with 0 indicating failure, or null if
the input array of paths is null.
public final boolean isUpToDate()
public final String[] getLocales()
On SDK 21 (Android 5.0: Lollipop) and above, Locale strings are valid
BCP-47 language tags and can be
parsed using Locale.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[] getNonSystemLocales()
public final Configuration[] getSizeConfigurations()
public final void setConfiguration(int mcc, int mnc, String locale, int orientation, int touchscreen, int density, int keyboard, int keyboardHidden, int navigation, int screenWidth, int screenHeight, int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp, int screenLayout, int uiMode, int majorVersion)
public final String getCookieName(int cookie)
public final SparseArray<String> getAssignedPackageIdentifiers()
public static final int getGlobalAssetCount()
public static final String getAssetAllocations()
public static final int getGlobalAssetManagerCount()