资源管理

导入模块

import resmgr from '@ohos.resmgr';

权限

方法

getResourceManager

获取当前应用的资源管理对象

参数:

参数名

类型

必填

说明

callback

AsyncCallback<ResourceManager>

callback方式返回ResourceManager对象

返回值:

示例:

resmgr.getResourceManager((error, mgr) => {
    // callback
    mgr.getString(0x1000000, (error, value) => {
        if (error != null) {
            console.log(value);
        } else {
            console.log(value);
        }
    });

    // promise
    mgr.getString(0x1000000).then(value => {
        console.log(value);
    }).catch(error => {
        console.log("getstring promise " + error);
    });
}

getResourceManager

获取指定应用的资源管理对象。

参数:

参数名

类型

必填

说明

bundleName

string

指定应用的Bundle名称

callback

AsyncCallback<ResourceManager>

callback方式返回ResourceManager对象

返回值:

示例:

resmgr.getResourceManager("com.example.myapplication", (error, mgr) => {
}

getResourceManager

获取当前应用的资源管理对象。

参数:

返回值:

类型

说明

Promise<ResourceManager>

Promise方式返回的资源管理对象

示例:

resmgr.getResourceManager().then(mgr => {
    // callback
    mgr.getString(0x1000000, (error, value) => {
        if (error != null) {
            console.log(value);
        } else {
            console.log(value);
        }
    });

    // promise
    mgr.getString(0x1000000).then(value => {
        console.log(value);
    }).catch(error => {
        console.log("getstring promise " + error);
    });
}).catch(error => {

});

getResourceManager

获取指定应用的资源管理对象。

参数:

参数名

类型

必填

说明

bundleName

string

指定应用的Bundle名称

返回值:

类型

说明

Promise<ResourceManager>

Promise方式返回的资源管理对象

示例:

resmgr.getResourceManager("com.example.myapplication").then(mgr => {

}).catch(error => {

});

枚举

Direction

用于表示设备屏幕方向。

名称

默认值

说明

DIRECTION_VERTICAL

0

Indicates the vertical direction.

DIRECTION_HORIZONTAL

1

Indicates the horizontal direction.

DeviceType

用于表示当前设备类型。

名称

默认值

说明

DEVICE_TYPE_PHONE

0x00

Indicates a smartphone.

DEVICE_TYPE_TABLET

0x01

Indicates a tablet.

DEVICE_TYPE_CAR

0x02

Indicates a car.

DEVICE_TYPE_PC

0x03

Indicates a PC.

DEVICE_TYPE_TV

0x04

Indicates a smart TV.

DEVICE_TYPE_WEARABLE

0x06

Indicates a wearable device.

ScreenDensity

用于表示当前设备屏幕密度。

名称

默认值

说明

SCREEN_SDPI

120

Indicates small screen density.

SCREEN_MDPI

160

Indicates medium screen density.

SCREEN_LDPI

240

Indicates large screen density.

SCREEN_XLDPI

320

Indicates extra-large screen density.

SCREEN_XXLDPI

480

Indicates extra-extra-large screen density.

SCREEN_XXXLDPI

640

Indicates extra-extra-extra-large screen density.

附录

AsyncCallback<T>

名称

参数类型

必填

说明

error

Error

执行不正常时返回错误对象,正常时无值

data

T

执行正常时返回T类型的对象,不正常时无值

Configuration

表示当前设备的状态。

属性

名称

参数类型

可读

可写

说明

direction

Direction

当前设备屏幕方向

locale

string

当前系统语言,如zh-Hans-CN

DeviceCapability

表示设备支持的。

属性

名称

参数类型

可读

可写

说明

screenDensity

ScreenDensity

当前设备屏幕密度

deviceType

DeviceType

当前设备类型

ResourceManager

提供访问应用资源的能力。

getString

callback接口,用户获取指定资源ID对应的字符串。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

callback

AsyncCallback<string>

异步回调,用于返回获取的字符串

返回值:

getString

promise接口,用户获取指定资源ID对应的字符串。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

返回值:

类型

说明

Promise<string>

资源ID值对应的字符串

getStringArray

callback接口,用户获取指定资源ID对应的字符串数组。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

callback

AsyncCallback<Array<string>>

异步回调,用于返回获取的字符串数组

返回值:

getStringArray

Promise接口,用户获取指定资源ID对应的字符串数组。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

返回值:

类型

说明

Promise<Array<string>>

资源ID值对应的字符串数组

getMedia

callback接口,用户获取指定资源ID对应的媒体文件内容。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

callback

AsyncCallback<Array<Uint8Array>>

异步回调,用于返回获取的媒体文件内容

返回值:

getMedia

Promise接口,用户获取指定资源ID对应的媒体文件内容。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

返回值:

类型

说明

Promise<Array<Uint8Array>>

资源ID值对应的媒体文件内容

getMediaBase64

callback接口,用户获取指定资源ID对应的图片资源Base64编码。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

callback

AsyncCallback<string>

异步回调,用于返回获取的图片资源Base64编码

返回值:

getMediaBase64

Promise接口,用户获取指定资源ID对应的图片资源Base64编码。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

返回值:

类型

说明

Promise<string>

资源ID值对应的图片资源Base64编码

getConfiguration

callback接口,用户获取设备的Configuration。

参数:

参数名

类型

必填

说明

callback

AsyncCallback<Configuration>

异步回调,用于返回设备的Configuration

返回值:

getConfiguration

Promise接口,用户获取设备的Configuration。

参数:

返回值:

类型

说明

Promise<Configuration>

设备的Configuration

getDeviceCapability

callback接口,用户获取设备的DeviceCapability。

参数:

参数名

类型

必填

说明

callback

AsyncCallback<DeviceCapability>

异步回调,用于返回设备的DeviceCapability

返回值:

getDeviceCapability

Promise接口,用户获取设备的DeviceCapability。

参数:

返回值:

类型

说明

Promise<DeviceCapability>

设备的DeviceCapability

返回值:

getPluralString

callback接口,根据提供的数量获取对应ID字符串表示的单复数字符串。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

num:

number

数量值

callback

AsyncCallback<string>

异步回调,返回根据提供的数量获取对应ID字符串表示的单复数字符串

返回值:

getPluralString

Promise接口,根据提供的数量获取对应ID字符串表示的单复数字符串。

参数:

参数名

类型

必填

说明

resId

number

资源ID值

num:

number

数量值

返回值:

类型

说明

Promise<string>

根据提供的数量获取对应ID字符串表示的单复数字符串