Skip to content
Search

Android.content.res.Resources

lastmod 2018-08-17created 2016-05-262 edits

Providing Resources

You should always externalize application resources such as images and strings from your code, so that you can maintain them independently. You should also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. At runtime, Android uses the appropriate resource based on the current configuration. For example, you might want to provide a different UI layout depending on the screen size or different strings depending on the language setting.

Once you externalize your application resources, you can access them using resource IDs that are generated in your project's R class. How to use resources in your application is discussed in Accessing Resources. This document shows you how to group your resources in your Android project and provide alternative resources for specific device configurations.

Describes how resources are provided in Android.

animator/

XML files that define property animations.

anim/

XML files that define tween animations. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.)

color/

XML files that define a state list of colors. See Color State List Resource.

drawable/

Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes

layout/

XML files that define a user interface layout. See Layout Resource.

menu/

XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource.

raw/

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

values/

XML files that contain simple values, such as strings, integers, and colors.

xml/

Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various XML configuration files must be saved here, such as a searchable configuration.

Methods

Lists the member functions.

getXML

openRawResource

When reading a raw resource file, you can get an input stream with Resources.openRawResource().

InputStream is = getResources().openRawResource(R.raw.aaa1);

Raw folder url path?

If a file_name.zip file exists in the raw folder, the URI is as follows.

Uri uri = "android.resource://" + getPackageName() + "/raw/file_name";

getIdentifier

public int getIdentifier(
      String name // 원하는 리소스의 이름.
    , String defType // 옵션사항으로 name에 "type/"이 포함되지 않은 경우 검색을 위한 기본 리소스 type. name에 type이 명시될 수 있도록 null 이 사용될 수 있습니다. 
    , String defPackage // 옵션사항으로 name에 "package:"가 포함되지 않은 경우 검색을 위한 기본 패키지 경로. name에 package가 명시될 수 있도록 null 이 사용될 수 있습니다.
)

The function above returns the resource identifier for the given resource name. A fully qualified resource name has the form package:type/entry. If defType and defPackage are specified, the first two components of the resource name (package and type) are optional.

Returns the associated resource identifier as an int. Returns 0 if the resource cannot be found. (0 is not a valid resource ID.) Simple usage is as follows.

int resId = contex.getResources().getIdentifier(filename, "raw", "android.guyentec.lifeanddead_lite");

See also

Favorite site