Students who have been exposed to Android development know that accessing program resources in Android is basically accessed through resource ID. This is very simple to develop, and it is possible to explicitly specify different resources such as various resolutions and languages without considering explicit specifications.
Pain points
However, sometimes there are some problems, such as we take pictures based on the server-side value, but the server-side will never return us the resource id, at most it is a value associated with the file name. When there are fewer operating resources, a container can be maintained to map the value and resource ID, but if there are too many, you need to find another way.
A convenient way
In this case, using the file name to get the resource ID seems to be twice the result with half the effort. The resource ID can be easily obtained by calling the getIdentifier of Resources. A few simple examples:
Resources res = getResources();
final String packageName = getPackageName();
int imageResId = ("ic_launcher", "drawable", packageName);
int imageResIdByAnotherForm = (packageName + ":drawable/ic_launcher", null, null);
int musicResId = ("test", "raw", packageName);
int notFoundResId = ("activity_main", "drawable", packageName);
(LOGTAG, "testGetResourceIds imageResId = " + imageResId
+ ";imageResIdByAnotherForm = " + imageResIdByAnotherForm
+ ";musicResId=" + musicResId
+ ";notFoundResId =" + notFoundResId);
Running results
I/MainActivity( 4537): testGetResourceIds imageResId = 2130837504;imageResIdByAnotherForm = 2130837504;musicResId=2130968576;notFoundResId =0
Take a look at the API
Direct API
1. This method is used to obtain the resource ID using the resource name
2. The complete resource name is package:type/entry. If the resource name parameter is completely specified, the subsequent defType and defPackage can be omitted.
When the defPackage is omitted, it needs to be set to null
4. Note that this method is not recommended, because directly accessing resources through resource ID will be more efficient.
5. If the resource is not found, return 0, 0 is not the legal resource ID in the Android resource ID.
**
* Return a resource identifier for the given resource name. A fully
* qualified resource name is of the form "package:type/entry". The first
* two components (package and type) are optional if defType and
* defPackage, respectively, are specified here.
*
* <p>Note: use of this function is discouraged. It is much more
* efficient to retrieve resources by identifier than by name.
*
* @param name The name of the desired resource.
* @param defType Optional default resource type to find, if "type/" is
* not included in the name. Can be null to require an
* explicit type.
* @param defPackage Optional default package to find, if "package:" is
* not included in the name. Can be null to require an
* explicit package.
*
* @return int The associated resource identifier. Returns 0 if no such
* resource was found. (0 is not a valid resource ID.)
*/
public int getIdentifier(String name, String defType, String defPackage) {
try {
return (name);
} catch (Exception e) {
// Ignore
}
return (name, defType, defPackage);
}
Indirect API
In fact, the above API calls the native method in it.
/**
* Retrieve the resource identifier for the given resource name.
*/
/*package*/ native final int getResourceIdentifier(String type,
String name,
String defPackage);