Skip to content

Android.content.res.Resources

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.

안드로이드의 리소스 제공방법에 대하여 설명한다.

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

멤버 함수 목록을 나열한다.

getXML

openRawResource

raw 리소스파일을 읽을 때, Resources.openRawResource()를 사용하여 입력스트림을 획득할 수 있다.

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

Raw folder url path?

만약, file_name.zip 파일이 raw 폴더에 존재할 경우 아래와 같은 URI가 된다.

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 이 사용될 수 있습니다.
)

위의 함수는 주어진 리소스 이름에 대한 리소스 식별자를 반환한다. 정규화된 리소스 이름은 package:type/entry 형태이다. defTypedefPackage가 명시되어 있다면 리소스 이름 처음의 두 구성요소(packagetype)는 옵션이다.

관련 리소스 식별자 int형을 반환한다. 리소스를 찾을 수 없다면 0을 반환합니다. (0은 유효한 리소스 ID가 아니다) 간단한 사용방법은 아래와 같다.

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

See also

Favorite site