public class Resources extends Object
getAssets()) and
provides a high-level API for getting typed data from the assets.
The Android resource system keeps track of all non-code assets associated with an
application. You can use this class to access your application's resources. You can generally
acquire the Resources instance associated with your application
with getResources().
The Android SDK tools compile your application's resources into the application binary
at build time. To use a resource, you must install it correctly in the source tree (inside
your project's res/ directory) and build your application. As part of the build
process, the SDK tools generate symbols for each resource, which you can use in your application
code to access the resources.
Using application resources makes it easy to update various characteristics of your application without modifying code, and—by providing sets of alternative resources—enables you to optimize your application for a variety of device configurations (such as for different languages and screen sizes). This is an important aspect of developing Android applications that are compatible on different types of devices.
For more information about using resources, see the documentation about Application Resources.
| Modifier and Type | Class and Description |
|---|---|
static class |
Resources.NotFoundException
This exception is thrown by the resource APIs when a requested resource
can not be found.
|
| Constructor and Description |
|---|
Resources(AssetManager assets,
DisplayMetrics metrics,
Configuration config)
Create a new Resources object on top of an existing set of assets in an
AssetManager.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
AssetManager |
getAssets()
Retrieve underlying AssetManager storage for these resources.
|
Configuration |
getConfiguration()
Return the current configuration that is in effect for this resource
object.
|
int |
getIdentifier(String name,
String defType,
String defPackage)
Return a resource identifier for the given resource name.
|
String |
getResourceEntryName(int resid)
Return the entry name for a given resource identifier.
|
void |
getValue(int id,
TypedValue outValue,
boolean resolveRefs)
Return the raw data associated with a particular resource ID.
|
InputStream |
openRawResource(int id)
Open a data stream for reading a raw resource.
|
InputStream |
openRawResource(int id,
TypedValue value)
Open a data stream for reading a raw resource.
|
void |
updateConfiguration(Configuration config,
DisplayMetrics metrics)
Store the newly updated configuration.
|
static void |
updateSystemConfiguration(Configuration config,
DisplayMetrics metrics)
Update the system resources configuration if they have previously
been initialized.
|
public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)
assets - Previously created AssetManager.metrics - Current display metrics to consider when
selecting/computing resource values.config - Desired device configuration to consider when
selecting/computing resource values (optional).public InputStream openRawResource(@RawRes int id) throws Resources.NotFoundException
id - The resource identifier to open, as generated by the appt
tool.Resources.NotFoundException - Throws NotFoundException if the given ID does not exist.public int getIdentifier(String name, String defType, String defPackage)
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
name - The name of the desired resource.defType - Optional default resource type to find, if "type/" is
not included in the name. Can be null to require an
explicit type.defPackage - Optional default package to find, if "package:" is
not included in the name. Can be null to require an
explicit package.public void getValue(@AnyRes int id, TypedValue outValue, boolean resolveRefs) throws Resources.NotFoundException
id - The desired resource identifier, as generated by the aapt
tool. This integer encodes the package, type, and resource
entry. The value 0 is an invalid identifier.outValue - Object in which to place the resource data.resolveRefs - If true, a resource that is a reference to another
resource will be followed so that you receive the
actual final resource data. If false, the TypedValue
will be filled in with the reference itself.Resources.NotFoundException - Throws NotFoundException if the given ID does not exist.public InputStream openRawResource(@RawRes int id, TypedValue value) throws Resources.NotFoundException
id - The resource identifier to open, as generated by the appt tool.value - The TypedValue object to hold the resource information.Resources.NotFoundException - Throws NotFoundException if the given ID does not exist.protected void finalize()
throws Throwable
ObjectNote 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.
public void updateConfiguration(Configuration config, DisplayMetrics metrics)
public static void updateSystemConfiguration(Configuration config, DisplayMetrics metrics)
public Configuration getConfiguration()
public String getResourceEntryName(@AnyRes int resid) throws Resources.NotFoundException
resid - The resource identifier whose entry name is to be
retrieved.Resources.NotFoundException - Throws NotFoundException if the given ID does not exist.#getResourceNamepublic final AssetManager getAssets()