資源管理

2024-01-23 15:52 更新

資源管理模塊,根據(jù)當前configuration:語言、區(qū)域、橫豎屏、Mcc(移動國家碼)和Mnc(移動網(wǎng)絡碼)、Device capability(設備類型)、Density(分辨率)提供獲取應用資源信息讀取接口。

說明

本模塊首批接口從API version 6開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

導入模塊

  1. import resourceManager from '@ohos.resourceManager';

使用說明

從API Version9開始,Stage模型通過context獲取resourceManager對象的方式后,可直接調用其內部獲取資源的接口,無需再導入包。此方式FA模型不適用,F(xiàn)A模型還需要先導入包,再調用getResourceManager接口獲取資源對象。

Stage模型下Context的引用方法請參考Stage模型的Context詳細介紹。

  1. import UIAbility from '@ohos.app.ability.UIAbility';
  2. export default class EntryAbility extends UIAbility {
  3. onWindowStageCreate(windowStage) {
  4. let context = this.context;
  5. let resourceManager = context.resourceManager;
  6. }
  7. }

resourceManager.getResourceManager

getResourceManager(callback: AsyncCallback<ResourceManager>): void

獲取當前應用的資源管理對象,使用callback形式返回ResourceManager對象。

模型約束:此接口僅可在FA模型下使用。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<ResourceManager>

callback方式返回ResourceManager對象

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. if (error != null) {
  3. console.log("error is " + error);
  4. return;
  5. }
  6. mgr.getString(0x1000000, (error, value) => {
  7. if (error != null) {
  8. console.log("error is " + error);
  9. } else {
  10. let str = value;
  11. }
  12. });
  13. });

注:示例代碼中的0x1000000表示資源對應的id, 其可在編譯后的文件ResourceTable.txt中找到。

resourceManager.getResourceManager

getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void

獲取指定應用的資源管理對象,使用callback形式返回ResourceManager對象。

模型約束:此接口僅可在FA模型下使用。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

bundleName

string

指定應用的Bundle名稱

callback

AsyncCallback<ResourceManager>

callback方式返回ResourceManager對象

示例:

  1. resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => {
  2. });

resourceManager.getResourceManager

getResourceManager(): Promise<ResourceManager>

獲取當前應用的資源管理對象,使用Promise形式返回ResourceManager對象。

模型約束:此接口僅可在FA模型下使用。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

返回值:

類型

說明

Promise<ResourceManager>

Promise方式返回資源管理對象

示例:

  1. resourceManager.getResourceManager().then(mgr => {
  2. mgr.getString(0x1000000, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let str = value;
  7. }
  8. });
  9. }).catch(error => {
  10. console.log("error is " + error);
  11. });

注:示例代碼中的0x1000000表示資源對應的id, 其可在編譯后的文件ResourceTable.txt中找到。

resourceManager.getResourceManager

getResourceManager(bundleName: string): Promise<ResourceManager>

獲取指定應用的資源管理對象,使用Promise形式返回ResourceManager對象。

模型約束:此接口僅可在FA模型下使用。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

bundleName

string

指定應用的Bundle名稱

返回值:

類型

說明

Promise<ResourceManager>

Promise方式返回的資源管理對象

示例:

  1. resourceManager.getResourceManager("com.example.myapplication").then(mgr => {
  2. }).catch(error => {
  3. });

Direction

用于表示設備屏幕方向。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

名稱

說明

DIRECTION_VERTICAL

0

豎屏

DIRECTION_HORIZONTAL

1

橫屏

DeviceType

用于表示當前設備類型。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

名稱

說明

DEVICE_TYPE_PHONE

0x00

手機

DEVICE_TYPE_TABLET

0x01

平板

DEVICE_TYPE_CAR

0x02

汽車

DEVICE_TYPE_PC

0x03

電腦

DEVICE_TYPE_TV

0x04

電視

DEVICE_TYPE_WEARABLE

0x06

穿戴

ScreenDensity

用于表示當前設備屏幕密度。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

名稱

說明

SCREEN_SDPI

120

小規(guī)模的屏幕密度

SCREEN_MDPI

160

中規(guī)模的屏幕密度

SCREEN_LDPI

240

大規(guī)模的屏幕密度

SCREEN_XLDPI

320

特大規(guī)模的屏幕密度

SCREEN_XXLDPI

480

超大規(guī)模的屏幕密度

SCREEN_XXXLDPI

640

超特大規(guī)模的屏幕密度

Configuration

表示當前設備的狀態(tài)。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

名稱

類型

可讀

可寫

說明

direction

Direction

當前設備屏幕方向

locale

string

當前系統(tǒng)語言

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getConfiguration((error, value) => {
  3. let direction = value.direction;
  4. let locale = value.locale;
  5. });
  6. });

DeviceCapability

表示設備支持的能力。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

名稱

類型

可讀

可寫

說明

screenDensity

ScreenDensity

當前設備屏幕密度

deviceType

DeviceType

當前設備類型

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getDeviceCapability((error, value) => {
  3. let screenDensity = value.screenDensity;
  4. let deviceType = value.deviceType;
  5. });
  6. });

RawFileDescriptor8+

表示rawfile的descriptor信息。

系統(tǒng)能力: SystemCapability.Global.ResourceManager

參數(shù):

名稱

類型

可讀

可寫

說明

fd

number

rawfile的descriptor

offset

number

rawfile的起始偏移量

length

number

rawfile的文件長度

Resource9+

表示的資源信息。

系統(tǒng)能力: 以下各項對應的系統(tǒng)能力均為SystemCapability.Global.ResourceManager

參數(shù):

名稱

類型

可讀

可寫

說明

bundleName

string

應用的bundle名稱

moduleName

string

應用的module名稱

id

number

資源的id值

ResourceManager

提供訪問應用資源的能力。

說明
  • ResourceManager涉及到的方法,僅限基于TS擴展的聲明式開發(fā)范式使用。

  • 資源文件在工程的resources目錄中定義,id可通過$r(資源地址).id的方式獲取,例如$r('app.string.test').id。

  • 對于本應用包資源,通過指定資源ID或資源名稱進行訪問。對于應用內跨包資源,通過指定resource對象或對應包的context進行訪問,通過對應包context訪問跨包資源與訪問本應用包資源的邏輯一致,推薦開發(fā)者使用對應包的context方法。

getStringValue9+

getStringValue(resId: number, callback: AsyncCallback<string>): void

用戶獲取指定資源ID對應的字符串,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

callback

AsyncCallback<string>

異步回調,用于返回獲取的字符串

錯誤碼:

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼ID

錯誤信息

9001001

If the module resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例Stage:

  1. try {
  2. this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let str = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getStringValue9+

getStringValue(resId: number): Promise<string>

用戶獲取指定資源ID對應的字符串,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

Promise<string>

資源ID值對應的字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringValue($r('app.string.test').id).then(value => {
  3. let str = value;
  4. }).catch(error => {
  5. console.log("getStringValue promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getStringValue9+

getStringValue(resource: Resource, callback: AsyncCallback<string>): void

用戶獲取指定resource對象對應的字符串,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

callback

AsyncCallback<string>

異步回調,用于返回獲取的字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.string.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getStringValue(resource, (error, value) => {
  8. if (error != null) {
  9. console.log("error is " + error);
  10. } else {
  11. let str = value;
  12. }
  13. });
  14. } catch (error) {
  15. console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  16. }

getStringValue9+

getStringValue(resource: Resource): Promise<string>

用戶獲取指定resource對象對應的字符串,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

返回值:

類型

說明

Promise<string>

resource對象對應的字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.string.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getStringValue(resource).then(value => {
  8. let str = value;
  9. }).catch(error => {
  10. console.log("getStringValue promise error is " + error);
  11. });
  12. } catch (error) {
  13. console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  14. }

getStringArrayValue9+

getStringArrayValue(resId: number, callback: AsyncCallback<Array<string>>): void

用戶獲取指定資源ID對應的字符串數(shù)組,使用callback形式返回字符串數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

callback

AsyncCallback<Array<string>>

異步回調,用于返回獲取的字符串數(shù)組

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let strArray = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getStringArrayValue9+

getStringArrayValue(resId: number): Promise<Array<string>>

用戶獲取指定資源ID對應的字符串數(shù)組,使用Promise形式返回字符串數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

Promise<Array<string>>

資源ID值對應的字符串數(shù)組

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then(value => {
  3. let strArray = value;
  4. }).catch(error => {
  5. console.log("getStringArrayValue promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getStringArrayValue9+

getStringArrayValue(resource: Resource, callback: AsyncCallback<Array<string>>): void

用戶獲取指定resource對象對應的字符串數(shù)組,使用callback形式返回回字符串數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

callback

AsyncCallback<Array<string>>

異步回調,用于返回獲取的字符串數(shù)組

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.strarray.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getStringArrayValue(resource, (error, value) => {
  8. if (error != null) {
  9. console.log("error is " + error);
  10. } else {
  11. let strArray = value;
  12. }
  13. });
  14. } catch (error) {
  15. console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
  16. }

getStringArrayValue9+

getStringArrayValue(resource: Resource): Promise<Array<string>>

用戶獲取指定resource對象對應的字符串數(shù)組,使用Promise形式返回字符串數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

返回值:

類型

說明

Promise<Array<string>>

resource對象對應的字符串數(shù)組

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.strarray.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getStringArrayValue(resource).then(value => {
  8. let strArray = value;
  9. }).catch(error => {
  10. console.log("getStringArray promise error is " + error);
  11. });
  12. } catch (error) {
  13. console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
  14. }

getMediaContent9+

getMediaContent(resId: number, callback: AsyncCallback<Uint8Array>): void

用戶獲取指定資源ID對應的媒體文件內容,使用callback形式返回字節(jié)數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

callback

AsyncCallback<Uint8Array>

異步回調,用于返回獲取的媒體文件內容

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

示例:

  1. try {
  2. this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let media = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getMediaContent9+

getMediaContent(resId: number): Promise<Uint8Array>

用戶獲取指定資源ID對應的媒體文件內容,使用Promise形式返回字節(jié)數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

Promise<Uint8Array>

資源ID值對應的媒體文件內容

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

示例:

  1. try {
  2. this.context.resourceManager.getMediaContent($r('app.media.test').id).then(value => {
  3. let media = value;
  4. }).catch(error => {
  5. console.log("getMediaContent promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getMediaContent9+

getMediaContent(resource: Resource, callback: AsyncCallback<Uint8Array>): void

用戶獲取指定resource對象對應的媒體文件內容,使用callback形式返回字節(jié)數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

callback

AsyncCallback<Uint8Array>

異步回調,用于返回獲取的媒體文件內容

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.media.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getMediaContent(resource, (error, value) => {
  8. if (error != null) {
  9. console.log("error is " + error);
  10. } else {
  11. let media = value;
  12. }
  13. });
  14. } catch (error) {
  15. console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
  16. }

getMediaContent9+

getMediaContent(resource: Resource): Promise<Uint8Array>

用戶獲取指定resource對象對應的媒體文件內容,使用Promise形式返回字節(jié)數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

返回值:

類型

說明

Promise<Uint8Array>

resource對象對應的媒體文件內容

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.media.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getMediaContent(resource).then(value => {
  8. let media = value;
  9. }).catch(error => {
  10. console.log("getMediaContent promise error is " + error);
  11. });
  12. } catch (error) {
  13. console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
  14. }

getMediaContentBase649+

getMediaContentBase64(resId: number, callback: AsyncCallback<string>): void

用戶獲取指定資源ID對應的圖片資源Base64編碼,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

callback

AsyncCallback<string>

異步回調,用于返回獲取的圖片資源Base64編碼

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

示例:

  1. try {
  2. this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let media = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getMediaContentBase649+

getMediaContentBase64(resId: number): Promise<string>

用戶獲取指定資源ID對應的圖片資源Base64編碼,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

Promise<string>

資源ID值對應的圖片資源Base64編碼

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

示例:

  1. try {
  2. this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then(value => {
  3. let media = value;
  4. }).catch(error => {
  5. console.log("getMediaContentBase64 promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getMediaContentBase649+

getMediaContentBase64(resource: Resource, callback: AsyncCallback<string>): void

用戶獲取指定resource對象對應的圖片資源Base64編碼,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

callback

AsyncCallback<string>

異步回調,用于返回獲取的圖片資源Base64編碼

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.media.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getMediaContentBase64(resource, (error, value) => {
  8. if (error != null) {
  9. console.log("error is " + error);
  10. } else {
  11. let media = value;
  12. }
  13. });
  14. } catch (error) {
  15. console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
  16. }

getMediaContentBase649+

getMediaContentBase64(resource: Resource): Promise<string>

用戶獲取指定resource對象對應的圖片資源Base64編碼,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

返回值:

類型

說明

Promise<string>

resource對象對應的圖片資源Base64編碼

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.media.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getMediaContentBase64(resource).then(value => {
  8. let media = value;
  9. }).catch(error => {
  10. console.log("getMediaContentBase64 promise error is " + error);
  11. });
  12. } catch (error) {
  13. console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
  14. }

getConfiguration

getConfiguration(callback: AsyncCallback<Configuration>): void

用戶獲取設備的Configuration,使用callback形式返回Configuration對象。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<Configuration>

異步回調,用于返回設備的Configuration

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getConfiguration((error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let direction = value.direction;
  7. let locale = value.locale;
  8. }
  9. });
  10. });

getConfiguration

getConfiguration(): Promise<Configuration>

用戶獲取設備的Configuration,使用Promise形式返回Configuration對象。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

返回值:

類型

說明

Promise<Configuration>

設備的Configuration

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getConfiguration().then(value => {
  3. let direction = value.direction;
  4. let locale = value.locale;
  5. }).catch(error => {
  6. console.log("getConfiguration promise error is " + error);
  7. });
  8. });

getDeviceCapability

getDeviceCapability(callback: AsyncCallback<DeviceCapability>): void

用戶獲取設備的DeviceCapability,使用callback形式返回DeviceCapability對象。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<DeviceCapability>

異步回調,用于返回設備的DeviceCapability

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getDeviceCapability((error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let screenDensity = value.screenDensity;
  7. let deviceType = value.deviceType;
  8. }
  9. });
  10. });

getDeviceCapability

getDeviceCapability(): Promise<DeviceCapability>

用戶獲取設備的DeviceCapability,使用Promise形式返回DeviceCapability對象。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

返回值:

類型

說明

Promise<DeviceCapability>

設備的DeviceCapability

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getDeviceCapability().then(value => {
  3. let screenDensity = value.screenDensity;
  4. let deviceType = value.deviceType;
  5. }).catch(error => {
  6. console.log("getDeviceCapability promise error is " + error);
  7. });
  8. });

getPluralStringValue9+

getPluralStringValue(resId: number, num: number, callback: AsyncCallback<string>): void

根據(jù)指定數(shù)量獲取指定ID字符串表示的單復數(shù)字符串,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

num

number

數(shù)量值

callback

AsyncCallback<string>

異步回調,返回根據(jù)指定數(shù)量獲取指定ID字符串表示的單復數(shù)字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let str = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getPluralStringValue9+

getPluralStringValue(resId: number, num: number): Promise<string>

根據(jù)指定數(shù)量獲取對指定ID字符串表示的單復數(shù)字符串,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

num

number

數(shù)量值

返回值:

類型

說明

Promise<string>

根據(jù)提供的數(shù)量獲取對應ID字符串表示的單復數(shù)字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then(value => {
  3. let str = value;
  4. }).catch(error => {
  5. console.log("getPluralStringValue promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getPluralStringValue9+

getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback<string>): void

根據(jù)指定數(shù)量獲取指定resource對象表示的單復數(shù)字符串,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

num

number

數(shù)量值

callback

AsyncCallback<string>

異步回調,返回根據(jù)指定數(shù)量獲取指定resource對象表示的單復數(shù)字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.plural.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => {
  8. if (error != null) {
  9. console.log("error is " + error);
  10. } else {
  11. let str = value;
  12. }
  13. });
  14. } catch (error) {
  15. console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  16. }

getPluralString9+

getPluralString(resource: Resource, num: number): Promise<string>

根據(jù)指定數(shù)量獲取對指定resource對象表示的單復數(shù)字符串,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

num

number

數(shù)量值

返回值:

類型

說明

Promise<string>

根據(jù)提供的數(shù)量獲取對應resource對象表示的單復數(shù)字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.plural.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getPluralString(resource, 1).then(value => {
  8. let str = value;
  9. }).catch(error => {
  10. console.log("getPluralString promise error is " + error);
  11. });
  12. } catch (error) {
  13. console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  14. }

getRawFileContent9+

getRawFileContent(path: string, callback: AsyncCallback<Uint8Array>): void

用戶獲取resources/rawfile目錄下對應的rawfile文件內容,使用callback形式返回字節(jié)數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

callback

AsyncCallback<Uint8Array>

異步回調,用于返回獲取的rawfile文件內容

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001005

If the resource not found by path.

示例:

  1. try {
  2. this.context.resourceManager.getRawFileContent("test.xml", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let rawFile = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getRawFileContent9+

getRawFileContent(path: string): Promise<Uint8Array>

用戶獲取resources/rawfile目錄下對應的rawfile文件內容,使用Promise形式返回字節(jié)數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

返回值:

類型

說明

Promise<Uint8Array>

rawfile文件內容

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001005

If the resource not found by path.

示例:

  1. try {
  2. this.context.resourceManager.getRawFileContent("test.xml").then(value => {
  3. let rawFile = value;
  4. }).catch(error => {
  5. console.log("getRawFileContent promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getRawFd9+

getRawFd(path: string, callback: AsyncCallback<RawFileDescriptor>): void

用戶獲取resources/rawfile目錄下對應rawfile文件的descriptor,使用callback形式返回。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

callback

AsyncCallback<RawFileDescriptor>

異步回調,用于返回獲取的rawfile文件的descriptor

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001005

If the resource not found by path.

示例:

  1. try {
  2. this.context.resourceManager.getRawFd("test.xml", (error, value) => {
  3. if (error != null) {
  4. console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`);
  5. } else {
  6. let fd = value.fd;
  7. let offset = value.offset;
  8. let length = value.length;
  9. }
  10. });
  11. } catch (error) {
  12. console.error(`callback getRawFd failed, error code: ${error.code}, message: ${error.message}.`)
  13. };

getRawFd9+

getRawFd(path: string): Promise<RawFileDescriptor>

用戶獲取resources/rawfile目錄下對應rawfile文件的descriptor,使用Promise形式返回。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

返回值:

類型

說明

Promise<RawFileDescriptor>

rawfile文件descriptor

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001005

If the resource not found by path.

示例:

  1. try {
  2. this.context.resourceManager.getRawFd("test.xml").then(value => {
  3. let fd = value.fd;
  4. let offset = value.offset;
  5. let length = value.length;
  6. }).catch(error => {
  7. console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`);
  8. });
  9. } catch (error) {
  10. console.error(`promise getRawFd failed, error code: ${error.code}, message: ${error.message}.`);
  11. };

closeRawFileDescriptor8+

closeRawFileDescriptor(path: string, callback: AsyncCallback<void>): void

用戶關閉resources/rawfile目錄下rawfile文件的descriptor,使用callback形式返回。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

callback

AsyncCallback<void>

異步回調

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.closeRawFileDescriptor("test.xml", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. }
  6. });
  7. });

closeRawFileDescriptor8+

closeRawFileDescriptor(path: string): Promise<void>

用戶關閉resources/rawfile目錄下rawfile文件的descriptor,使用Promise形式返回。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

返回值:

類型

說明

Promise<void>

無返回值

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.closeRawFileDescriptor("test.xml").then(value => {
  3. let result = value;
  4. }).catch(error => {
  5. console.log("closeRawFileDescriptor promise error is " + error);
  6. });
  7. });

closeRawFd9+

closeRawFd(path: string, callback: AsyncCallback<void>): void

用戶關閉resources/rawfile目錄下rawfile文件的descriptor,使用callback形式返回。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

callback

AsyncCallback<void>

異步回調

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001005

The resource not found by path.

示例:

  1. try {
  2. this.context.resourceManager.closeRawFd("test.xml", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. }
  6. });
  7. } catch (error) {
  8. console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

closeRawFd9+

closeRawFd(path: string): Promise<void>

用戶關閉resources/rawfile目錄下rawfile文件的descriptor,使用Promise形式返回。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

返回值:

類型

說明

Promise<void>

無返回值

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001005

If the resource not found by path.

示例:

  1. try {
  2. this.context.resourceManager.closeRawFd("test.xml").then(value => {
  3. let result = value;
  4. }).catch(error => {
  5. console.log("closeRawFd promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

release7+

release()

用戶釋放創(chuàng)建的resourceManager。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.release();
  3. });

getStringByName9+

getStringByName(resName: string, callback: AsyncCallback<string>): void

用戶獲取指定資源名稱對應的字符串,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

callback

AsyncCallback<string>

異步回調,用于返回獲取的字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringByName("test", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let string = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getStringByName9+

getStringByName(resName: string): Promise<string>

用戶獲取指定資源名稱對應的字符串,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

返回值:

類型

說明

Promise<string>

資源名稱對應的字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringByName("test").then(value => {
  3. let string = value;
  4. }).catch(error => {
  5. console.log("getStringByName promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getStringArrayByName9+

getStringArrayByName(resName: string, callback: AsyncCallback<Array<string>>): void

用戶獲取指定資源名稱對應的字符串數(shù)組,使用callback形式返回字符串數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

callback

AsyncCallback<Array<string>>

異步回調,用于返回獲取的字符串數(shù)組

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringArrayByName("test", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let strArray = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getStringArrayByName9+

getStringArrayByName(resName: string): Promise<Array<string>>

用戶獲取指定資源名稱對應的字符串數(shù)組,使用Promise形式返回字符串數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

返回值:

類型

說明

Promise<Array<string>>

資源名稱對應的字符串數(shù)組

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringArrayByName("test").then(value => {
  3. let strArray = value;
  4. }).catch(error => {
  5. console.log("getStringArrayByName promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getMediaByName9+

getMediaByName(resName: string, callback: AsyncCallback<Uint8Array>): void

用戶獲取指定資源名稱對應的媒體文件內容,使用callback形式返回字節(jié)數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

callback

AsyncCallback<Uint8Array>

異步回調,用于返回獲取的媒體文件內容

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getMediaByName("test", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let media = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getMediaByName9+

getMediaByName(resName: string): Promise<Uint8Array>

用戶獲取指定資源名稱對應的媒體文件內容,使用Promise形式返回字節(jié)數(shù)組。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

返回值:

類型

說明

Promise<Uint8Array>

資源名稱對應的媒體文件內容

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getMediaByName("test").then(value => {
  3. let media = value;
  4. }).catch(error => {
  5. console.log("getMediaByName promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getMediaBase64ByName9+

getMediaBase64ByName(resName: string, callback: AsyncCallback<string>): void

用戶獲取指定資源名稱對應的圖片資源Base64編碼,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

callback

AsyncCallback<string>

異步回調,用于返回獲取的圖片資源Base64編碼

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getMediaBase64ByName("test", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let media = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getMediaBase64ByName9+

getMediaBase64ByName(resName: string): Promise<string>

用戶獲取指定資源名稱對應的圖片資源Base64編碼,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

返回值:

類型

說明

Promise<string>

資源名稱對應的圖片資源Base64編碼

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getMediaBase64ByName("test").then(value => {
  3. let media = value;
  4. }).catch(error => {
  5. console.log("getMediaBase64ByName promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getPluralStringByName9+

getPluralStringByName(resName: string, num: number, callback: AsyncCallback<string>): void

根據(jù)傳入的數(shù)量值,獲取資源名稱對應的字符串資源,使用callback形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

num

number

數(shù)量值

callback

AsyncCallback<string>

異步回調,返回根據(jù)傳入的數(shù)量值獲取資源名稱對應的字符串資源

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let str = value;
  7. }
  8. });
  9. } catch (error) {
  10. console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`)
  11. }

getPluralStringByName9+

getPluralStringByName(resName: string, num: number): Promise<string>

根據(jù)傳入的數(shù)量值,獲取資源名稱對應的字符串資源,使用Promise形式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

num

number

數(shù)量值

返回值:

類型

說明

Promise<string>

根據(jù)傳入的數(shù)量值獲取資源名稱對應的字符串資源

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getPluralStringByName("test", 1).then(value => {
  3. let str = value;
  4. }).catch(error => {
  5. console.log("getPluralStringByName promise error is " + error);
  6. });
  7. } catch (error) {
  8. console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getStringSync9+

getStringSync(resId: number): string

用戶獲取指定資源ID對應的字符串,使用同步方式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

string

資源ID值對應的字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringSync($r('app.string.test').id);
  3. } catch (error) {
  4. console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
  5. }

getStringSync9+

getStringSync(resource: Resource): string

用戶獲取指定resource對象對應的字符串,使用同步方式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

返回值:

類型

說明

string

resource對象對應的字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.string.test').id
  5. };
  6. try {
  7. this.context.resourceManager.getStringSync(resource);
  8. } catch (error) {
  9. console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
  10. }

getStringByNameSync9+

getStringByNameSync(resName: string): string

用戶獲取指定資源名稱對應的字符串,使用同步方式返回字符串。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

返回值:

類型

說明

string

資源名稱對應的字符串

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getStringByNameSync("test");
  3. } catch (error) {
  4. console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`)
  5. }

getBoolean9+

getBoolean(resId: number): boolean

使用同步方式,返回獲取指定資源ID對應的布爾結果。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

boolean

資源ID值對應的布爾結果

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id);
  3. } catch (error) {
  4. console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`)
  5. }

getBoolean9+

getBoolean(resource: Resource): boolean

使用同步方式,返回獲取指定resource對象對應的布爾結果。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

返回值:

類型

說明

boolean

resource對象對應的布爾結果

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.boolean.boolean_test').id
  5. };
  6. try {
  7. this.context.resourceManager.getBoolean(resource);
  8. } catch (error) {
  9. console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`)
  10. }

getBooleanByName9+

getBooleanByName(resName: string): boolean

使用同步方式,返回獲取指定資源名稱對應的布爾結果

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

返回值:

類型

說明

boolean

資源名稱對應的布爾結果

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getBooleanByName("boolean_test");
  3. } catch (error) {
  4. console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`)
  5. }

getNumber9+

getNumber(resId: number): number

用戶獲取指定資源ID對應的integer數(shù)值或者float數(shù)值,使用同步方式返回資源對應的數(shù)值。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

number

資源ID值對應的數(shù)值。Integer對應的是原數(shù)值,float對應的是真實像素點值,具體參考示例代碼。

以下錯誤碼的詳細介紹請參見資源管理錯誤碼。

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer對應返回的是原數(shù)值
  3. } catch (error) { console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
  4. }
  5. try {
  6. this.context.resourceManager.getNumber($r('app.float.float_test').id); // float對應返回的是真實像素點值
  7. } catch (error) { console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
  8. }

getNumber9+

getNumber(resource: Resource): number

用戶獲取指定resource對象對應的integer數(shù)值或者float數(shù)值,使用同步方式返回資源對應的數(shù)值。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resource

Resource

資源信息

返回值:

類型

說明

number

resource對象對應的數(shù)值。Integer對應的是原數(shù)值,float對應的是真實像素點值,具體參考示例代碼。

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001001

If the resId invalid.

9001002

If the resource not found by resId.

9001006

If the resource re-ref too much.

示例:

  1. let resource = {
  2. bundleName: "com.example.myapplication",
  3. moduleName: "entry",
  4. id: $r('app.integer.integer_test').id};
  5. try {
  6. this.context.resourceManager.getNumber(resource);// integer對應返回的是原數(shù)值, float對應返回的是真實像素點值
  7. } catch (error) {
  8. console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
  9. }

getNumberByName9+

getNumberByName(resName: string): number

用戶獲取指定資源名稱對應的integer數(shù)值或者float數(shù)值,使用同步方式資源對應的數(shù)值。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resName

string

資源名稱

返回值:

類型

說明

number

資源名稱對應的數(shù)值

以下錯誤碼的詳細介紹請參見資源管理錯誤碼

錯誤碼:

錯誤碼ID

錯誤信息

9001003

If the resName invalid.

9001004

If the resource not found by resName.

9001006

If the resource re-ref too much.

示例:

  1. try {
  2. this.context.resourceManager.getNumberByName("integer_test");
  3. } catch (error) {
  4. console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`)
  5. }
  6. try {
  7. this.context.resourceManager.getNumberByName("float_test");
  8. } catch (error) {
  9. console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`)
  10. }

getString(deprecated)

getString(resId: number, callback: AsyncCallback<string>): void

用戶獲取指定資源ID對應的字符串,使用callback形式返回字符串。

從API version 9開始不再維護,建議使用getStringValue代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

callback

AsyncCallback<string>

異步回調,用于返回獲取的字符串

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getString($r('app.string.test').id, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let str = value;
  7. }
  8. });
  9. });

getString(deprecated)

getString(resId: number): Promise<string>

用戶獲取指定資源ID對應的字符串,使用Promise形式返回字符串。

從API version 9開始不再維護,建議使用getStringValue代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

Promise<string>

資源ID值對應的字符串

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getString($r('app.string.test').id).then(value => {
  3. let str = value;
  4. }).catch(error => {
  5. console.log("getstring promise error is " + error);
  6. });
  7. });

getStringArray(deprecated)

getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void

用戶獲取指定資源ID對應的字符串數(shù)組,使用callback形式返回字符串數(shù)組。

從API version 9開始不再維護,建議使用getStringArrayValue代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

callback

AsyncCallback<Array<string>>

異步回調,用于返回獲取的字符串數(shù)組

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getStringArray($r('app.strarray.test').id, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let strArray = value;
  7. }
  8. });
  9. });

getStringArray(deprecated)

getStringArray(resId: number): Promise<Array<string>>

用戶獲取指定資源ID對應的字符串數(shù)組,使用Promise形式返回字符串數(shù)組。

從API version 9開始不再維護,建議使用getStringArrayValue代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

Promise<Array<string>>

資源ID值對應的字符串數(shù)組

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getStringArray($r('app.strarray.test').id).then(value => {
  3. let strArray = value;
  4. }).catch(error => {
  5. console.log("getStringArray promise error is " + error);
  6. });
  7. });

getMedia(deprecated)

getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void

用戶獲取指定資源ID對應的媒體文件內容,使用callback形式返回字節(jié)數(shù)組。

從API version 9開始不再維護,建議使用getMediaContent代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

callback

AsyncCallback<Uint8Array>

異步回調,用于返回獲取的媒體文件內容

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getMedia($r('app.media.test').id, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let media = value;
  7. }
  8. });
  9. });

getMedia(deprecated)

getMedia(resId: number): Promise<Uint8Array>

用戶獲取指定資源ID對應的媒體文件內容,使用Promise形式返回字節(jié)數(shù)組。

從API version 9開始不再維護,建議使用getMediaContent代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

Promise<Uint8Array>

資源ID值對應的媒體文件內容

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getMedia($r('app.media.test').id).then(value => {
  3. let media = value;
  4. }).catch(error => {
  5. console.log("getMedia promise error is " + error);
  6. });
  7. });

getMediaBase64(deprecated)

getMediaBase64(resId: number, callback: AsyncCallback<string>): void

用戶獲取指定資源ID對應的圖片資源Base64編碼,使用callback形式返回字符串。

從API version 9開始不再維護,建議使用getMediaContentBase64代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

callback

AsyncCallback<string>

異步回調,用于返回獲取的圖片資源Base64編碼

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getMediaBase64($r('app.media.test').id, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let media = value;
  7. }
  8. });
  9. });

getMediaBase64(deprecated)

getMediaBase64(resId: number): Promise<string>

用戶獲取指定資源ID對應的圖片資源Base64編碼,使用Promise形式返回字符串。

從API version 9開始不再維護,建議使用getMediaContentBase64代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

返回值:

類型

說明

Promise<string>

資源ID值對應的圖片資源Base64編碼

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getMediaBase64($r('app.media.test').id).then(value => {
  3. let media = value;
  4. }).catch(error => {
  5. console.log("getMediaBase64 promise error is " + error);
  6. });
  7. });

getPluralString(deprecated)

getPluralString(resId: number, num: number): Promise<string>

根據(jù)指定數(shù)量獲取對指定ID字符串表示的單復數(shù)字符串,使用Promise形式返回字符串。

從API version 9開始不再維護,建議使用getPluralStringValue代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

num

number

數(shù)量值

返回值:

類型

說明

Promise<string>

根據(jù)提供的數(shù)量獲取對應ID字符串表示的單復數(shù)字符串

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getPluralString($r("app.plural.test").id, 1).then(value => {
  3. let str = value;
  4. }).catch(error => {
  5. console.log("getPluralString promise error is " + error);
  6. });
  7. });

getPluralString(deprecated)

getPluralString(resId: number, num: number, callback: AsyncCallback<string>): void

根據(jù)指定數(shù)量獲取指定ID字符串表示的單復數(shù)字符串,使用callback形式返回字符串。

從API version 9開始不再維護,建議使用getPluralStringValue代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

resId

number

資源ID值

num

number

數(shù)量值

callback

AsyncCallback<string>

異步回調,返回根據(jù)指定數(shù)量獲取指定ID字符串表示的單復數(shù)字符串

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getPluralString($r("app.plural.test").id, 1, (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let str = value;
  7. }
  8. });
  9. });

getRawFile(deprecated)

getRawFile(path: string, callback: AsyncCallback<Uint8Array>): void

用戶獲取resources/rawfile目錄下對應的rawfile文件內容,使用callback形式返回字節(jié)數(shù)組。

從API version 9開始不再維護,建議使用getRawFileContent代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

callback

AsyncCallback<Uint8Array>

異步回調,用于返回獲取的rawfile文件內容

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getRawFile("test.xml", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let rawFile = value;
  7. }
  8. });
  9. });

getRawFile(deprecated)

getRawFile(path: string): Promise<Uint8Array>

用戶獲取resources/rawfile目錄下對應的rawfile文件內容,使用Promise形式返回字節(jié)數(shù)組。

從API version 9開始不再維護,建議使用getRawFileContent代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

返回值:

類型

說明

Promise<Uint8Array>

rawfile文件內容

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getRawFile("test.xml").then(value => {
  3. let rawFile = value;
  4. }).catch(error => {
  5. console.log("getRawFile promise error is " + error);
  6. });
  7. });

getRawFileDescriptor(deprecated)

getRawFileDescriptor(path: string, callback: AsyncCallback<RawFileDescriptor>): void

用戶獲取resources/rawfile目錄下對應rawfile文件的descriptor,使用callback形式返回。

從API version 9開始不再維護,建議使用getRawFd代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

callback

AsyncCallback<RawFileDescriptor>

異步回調,用于返回獲取的rawfile文件的descriptor

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getRawFileDescriptor("test.xml", (error, value) => {
  3. if (error != null) {
  4. console.log("error is " + error);
  5. } else {
  6. let fd = value.fd;
  7. let offset = value.offset;
  8. let length = value.length;
  9. }
  10. });
  11. });

getRawFileDescriptor(deprecated)

getRawFileDescriptor(path: string): Promise<RawFileDescriptor>

用戶獲取resources/rawfile目錄下對應rawfile文件的descriptor,使用Promise形式返回。

從API version 9開始不再維護,建議使用getRawFd代替。

系統(tǒng)能力:SystemCapability.Global.ResourceManager

參數(shù):

參數(shù)名

類型

必填

說明

path

string

rawfile文件路徑

返回值:

類型

說明

Promise<RawFileDescriptor>

rawfile文件descriptor

示例:

  1. resourceManager.getResourceManager((error, mgr) => {
  2. mgr.getRawFileDescriptor("test.xml").then(value => {
  3. let fd = value.fd;
  4. let offset = value.offset;
  5. let length = value.length;
  6. }).catch(error => {
  7. console.log("getRawFileDescriptor promise error is " + error);
  8. });
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號