harmony 鸿蒙@ohos.resourceManager (资源管理)

2022-08-09 浏览 (2191)

@ohos.resourceManager (资源管理)

资源管理模块,根据当前configuration:语言、区域、横竖屏、Mcc(移动国家码)和Mnc(移动网络码)、Device capability(设备类型)、Density(分辨率)提供获取应用资源信息读取接口。

说明:

本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import resourceManager from '@ohos.resourceManager';

使用说明

从API Version9开始,Stage模型通过context获取resourceManager对象的方式后,可直接调用其内部获取资源的接口,无需再导入包。此方式FA模型不适用,FA模型还需要先导入包,再调用getResourceManager接口获取资源对象。 Stage模型下Context的引用方法请参考Stage模型的Context详细介绍

import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage) {
    let context = this.context;
    let resourceManager = context.resourceManager;
  }
}

resourceManager.getResourceManager

getResourceManager(callback: AsyncCallback<ResourceManager>): void

获取当前应用的资源管理对象,使用callback形式返回ResourceManager对象。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在FA模型下使用。

参数:

参数名类型必填说明
callbackAsyncCallback<ResourceManager>callback方式返回ResourceManager对象

示例:

resourceManager.getResourceManager((error, mgr) => {
  if (error != null) {
    console.log("error is " + error);
    return;
  }
  mgr.getStringValue(0x1000000, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
});

注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。

resourceManager.getResourceManager

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

获取指定应用的资源管理对象,使用callback形式返回ResourceManager对象。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在FA模型下使用。

参数:

参数名类型必填说明
bundleNamestring指定应用的Bundle名称
callbackAsyncCallback<ResourceManager>callback方式返回ResourceManager对象

示例:

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

resourceManager.getResourceManager

getResourceManager(): Promise<ResourceManager>

获取当前应用的资源管理对象,使用Promise形式返回ResourceManager对象。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在FA模型下使用。

返回值:

类型说明
Promise<ResourceManager>Promise方式返回资源管理对象

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager().then((mgr: resourceManager.ResourceManager) => {
  mgr.getStringValue(0x1000000, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
}).catch((error: BusinessError) => {
  console.log("error is " + error);
});

注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。

resourceManager.getResourceManager

getResourceManager(bundleName: string): Promise<ResourceManager>

获取指定应用的资源管理对象,使用Promise形式返回ResourceManager对象。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在FA模型下使用。

参数:

参数名类型必填说明
bundleNamestring指定应用的Bundle名称

返回值:

类型说明
Promise<ResourceManager>Promise方式返回的资源管理对象

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager("com.example.myapplication").then((mgr: resourceManager.ResourceManager) => {
}).catch((error: BusinessError) => {
});

resourceManager.getSystemResourceManager10+

getSystemResourceManager(): ResourceManager

获取系统资源管理对象,返回系统资源的ResourceManager对象。

系统能力:SystemCapability.Global.ResourceManager

返回值:

类型说明
Resourcemanager返回系统资源的管理对象

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001009If application can't access system resource.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

try {
  let systemResourceManager = resourceManager.getSystemResourceManager();
  systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then((value: string) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("systemResourceManager getStringValue promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`systemResourceManager getStringValue failed, error code: ${code}, message: ${message}.`);
}

Direction

用于表示设备屏幕方向。

系统能力:SystemCapability.Global.ResourceManager

名称说明
DIRECTION_VERTICAL0竖屏
DIRECTION_HORIZONTAL1横屏

DeviceType

用于表示当前设备类型。

系统能力:SystemCapability.Global.ResourceManager

名称说明
DEVICE_TYPE_TABLET0x01平板
DEVICE_TYPE_CAR0x02汽车
DEVICE_TYPE_TV0x04电视
DEVICE_TYPE_WEARABLE0x06穿戴

ScreenDensity

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

系统能力:SystemCapability.Global.ResourceManager

名称说明
SCREEN_SDPI120小规模的屏幕密度
SCREEN_MDPI160中规模的屏幕密度
SCREEN_LDPI240大规模的屏幕密度
SCREEN_XLDPI320特大规模的屏幕密度
SCREEN_XXLDPI480超大规模的屏幕密度
SCREEN_XXXLDPI640超特大规模的屏幕密度

Configuration

表示当前设备的状态。

系统能力:SystemCapability.Global.ResourceManager

参数:

名称类型可读可写说明
directionDirection当前设备屏幕方向
localestring当前系统语言

DeviceCapability

表示设备支持的能力。

系统能力:SystemCapability.Global.ResourceManager

参数:

名称类型可读可写说明
screenDensityScreenDensity当前设备屏幕密度
deviceTypeDeviceType当前设备类型

RawFileDescriptor8+

表示rawfile的descriptor信息。

系统能力: SystemCapability.Global.ResourceManager

参数:

名称类型可读可写说明
fdnumberrawfile所在hap的文件描述符
offsetnumberrawfile的起始偏移量
lengthnumberrawfile的文件长度

Resource9+

表示的资源信息。

系统能力: 以下各项对应的系统能力均为SystemCapability.Global.ResourceManager

参数:

名称类型可读可写说明
bundleNamestring应用的bundle名称
moduleNamestring应用的module名称
idnumber资源的id值
paramsany[]其他资源参数(可选)
typenumber资源的类型(可选)

ResourceManager

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

说明:

  • ResourceManager涉及到的方法,仅限基于TS扩展的声明式开发范式使用。

  • 资源文件在工程的resources目录中定义,id可通过$r(资源地址).id的方式获取,例如$r('app.string.test').id。

getStringSync9+

getStringSync(resId: number): string

用户获取指定资源ID对应的字符串,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
string资源ID值对应的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringSync($r('app.string.test').id);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
}

getStringSync10+

getStringSync(resId: number, ...args: Array<string|number>): string

用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
argsArray<string |number>格式化字符串资源参数
支持参数类型:
%d、%f、%s、%%
说明:%%转译符,转译%
举例:%%d格式化后为%d字符串

返回值:

类型说明
string资源ID值对应的格式化字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.
9001007If the resource obtained by resId formatting error.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
}

getStringSync9+

getStringSync(resource: Resource): string

用户获取指定resource对象对应的字符串,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
stringresource对象对应的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.string.test').id
};
try {
  this.context.resourceManager.getStringSync(resource);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
}

getStringSync10+

getStringSync(resource: Resource, ...args: Array<string|number>): string

用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
argsArray<string |number>格式化字符串资源参数
支持参数类型:
%d、%f、%s、%%
说明:%%转译符,转译%
举例:%%d格式化后为%d字符串

返回值:

类型说明
stringresource对象对应的格式化字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.
9001007If the resource obtained by resId formatting error.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.string.test').id
};
try {
  this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
}

getStringByNameSync9+

getStringByNameSync(resName: string): string

用户获取指定资源名称对应的字符串,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
string资源名称对应的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringByNameSync("test");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`);
}

getStringByNameSync10+

getStringByNameSync(resName: string, ...args: Array<string|number>): string

用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
argsArray<string |number>格式化字符串资源参数
支持参数类型:
%d、%f、%s、%%
说明:%%转译符,转译%
举例:%%d格式化后为%d字符串

返回值:

类型说明
string资源名称对应的格式化字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.
9001008If the resource obtained by resName formatting error.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`);
}

getStringValue9+

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

用户获取指定资源ID对应的字符串,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<string>异步回调,用于返回获取的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the module resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例Stage:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getStringValue failed, error code: ${code}, message: ${message}.`);
}

getStringValue9+

getStringValue(resId: number): Promise<string>

用户获取指定资源ID对应的字符串,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<string>资源ID值对应的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringValue($r('app.string.test').id).then((value: string) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("getStringValue promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getStringValue failed, error code: ${code}, message: ${message}.`);
}

getStringValue9+

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

用户获取指定resource对象对应的字符串,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
callbackAsyncCallback<string>异步回调,用于返回获取的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.string.test').id
};
try {
  this.context.resourceManager.getStringValue(resource, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getStringValue failed, error code: ${code}, message: ${message}.`);
}

getStringValue9+

getStringValue(resource: Resource): Promise<string>

用户获取指定resource对象对应的字符串,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
Promise<string>resource对象对应的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.string.test').id
};
try {
  this.context.resourceManager.getStringValue(resource).then((value: string) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("getStringValue promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getStringValue failed, error code: ${code}, message: ${message}.`);
}

getStringByName9+

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

用户获取指定资源名称对应的字符串,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
callbackAsyncCallback<string>异步回调,用于返回获取的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringByName("test", (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getStringByName failed, error code: ${code}, message: ${message}.`);
}

getStringByName9+

getStringByName(resName: string): Promise<string>

用户获取指定资源名称对应的字符串,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
Promise<string>资源名称对应的字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringByName("test").then((value: string) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("getStringByName promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getStringByName failed, error code: ${code}, message: ${message}.`);
}

getStringArrayValueSync10+

getStringArrayValueSync(resId: number): Array<string>

用户获取指定资源ID对应的字符串数组,使用同步方式返回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Array<string>资源ID值对应的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringArrayValueSync($r('app.strarray.test').id);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`);
}

getStringArrayValueSync10+

getStringArrayValueSync(resource: Resource): Array<string>

用户获取指定resource对象对应的字符串数组,使用同步方式返回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
Array<string>resource对象对应的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.strarray.test').id
};
try {
  this.context.resourceManager.getStringArrayValueSync(resource);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`);
}

getStringArrayByNameSync10+

getStringArrayByNameSync(resName: string): Array<string>

用户获取指定资源名称对应的字符串数组,使用同步方式返回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
Array<string>对应资源名称的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

try {
  this.context.resourceManager.getStringArrayByNameSync("test");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getStringArrayByNameSync failed, error code: ${code}, message: ${message}.`);
}

getStringArrayValue9+

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

用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<Array<string>>异步回调,用于返回获取的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let strArray = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getStringArrayValue failed, error code: ${code}, message: ${message}.`);
}

getStringArrayValue9+

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

用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<Array<string>>资源ID值对应的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then((value: Array<string>) => {
    let strArray = value;
  }).catch((error: BusinessError) => {
    console.log("getStringArrayValue promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getStringArrayValue failed, error code: ${code}, message: ${message}.`);
}

getStringArrayValue9+

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

用户获取指定resource对象对应的字符串数组,使用callback形式返回回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
callbackAsyncCallback<Array<string>>异步回调,用于返回获取的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.strarray.test').id
};
try {
  this.context.resourceManager.getStringArrayValue(resource, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let strArray = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getStringArrayValue failed, error code: ${code}, message: ${message}.`);
}

getStringArrayValue9+

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

用户获取指定resource对象对应的字符串数组,使用Promise形式返回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
Promise<Array<string>>resource对象对应的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.strarray.test').id
};
try {
  this.context.resourceManager.getStringArrayValue(resource).then((value: Array<string>) => {
    let strArray = value;
  }).catch((error: BusinessError) => {
    console.log("getStringArray promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getStringArrayValue failed, error code: ${code}, message: ${message}.`);
}

getStringArrayByName9+

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

用户获取指定资源名称对应的字符串数组,使用callback形式返回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
callbackAsyncCallback<Array<string>>异步回调,用于返回获取的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringArrayByName("test", (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let strArray = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getStringArrayByName failed, error code: ${code}, message: ${message}.`);
}

getStringArrayByName9+

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

用户获取指定资源名称对应的字符串数组,使用Promise形式返回字符串数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
Promise<Array<string>>资源名称对应的字符串数组

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getStringArrayByName("test").then((value: Array<string>) => {
    let strArray = value;
  }).catch((error: BusinessError) => {
    console.log("getStringArrayByName promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getStringArrayByName failed, error code: ${code}, message: ${message}.`);
}

getPluralStringValueSync10+

getPluralStringValueSync(resId: number, num: number): string

根据指定数量获取指定ID字符串表示的单复数字符串,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
numnumber数量值

返回值:

类型说明
string根据指定数量获取指定ID字符串表示的单复数字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getPluralStringValueSync($r('app.plural.test').id, 1);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`);
}

getPluralStringValueSync10+

getPluralStringValueSync(resource: Resource, num: number): string

根据指定数量获取指定resource对象表示的单复数字符串,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
numnumber数量值

返回值:

类型说明
string根据指定数量获取指定resource对象表示的单复数字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.plural.test').id
};
try {
  this.context.resourceManager.getPluralStringValueSync(resource, 1);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`);
}

getPluralStringByNameSync10+

getPluralStringByNameSync(resName: string, num: number): string

根据指定数量获取指定资源名称表示的单复数字符串,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
numnumber数量值

返回值:

类型说明
string根据指定数量获取指定资源名称表示的单复数字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getPluralStringByNameSync("test", 1);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getPluralStringByNameSync failed, error code: ${code}, message: ${message}.`);
}

getPluralStringValue9+

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

根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
numnumber数量值
callbackAsyncCallback<string>异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getPluralStringValue failed, error code: ${code}, message: ${message}.`);
}

getPluralStringValue9+

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

根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
numnumber数量值

返回值:

类型说明
Promise<string>根据提供的数量获取对应ID字符串表示的单复数字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then((value: string) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("getPluralStringValue promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getPluralStringValue failed, error code: ${code}, message: ${message}.`);
}

getPluralStringValue9+

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

根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
numnumber数量值
callbackAsyncCallback<string>异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.plural.test').id
};
try {
  this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getPluralStringValue failed, error code: ${code}, message: ${message}.`);
}

getPluralStringValue9+

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

根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
numnumber数量值

返回值:

类型说明
Promise<string>根据提供的数量获取对应resource对象表示的单复数字符串

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.plural.test').id
};
try {
  this.context.resourceManager.getPluralStringValue(resource, 1).then((value: string) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("getPluralStringValue promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getPluralStringValue failed, error code: ${code}, message: ${message}.`);
}

getPluralStringByName9+

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

根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
numnumber数量值
callbackAsyncCallback<string>异步回调,返回根据传入的数量值获取资源名称对应的字符串资源

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getPluralStringByName failed, error code: ${code}, message: ${message}.`);
}

getPluralStringByName9+

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

根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
numnumber数量值

返回值:

类型说明
Promise<string>根据传入的数量值获取资源名称对应的字符串资源

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getPluralStringByName("test", 1).then((value: string) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("getPluralStringByName promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getPluralStringByName failed, error code: ${code}, message: ${message}.`);
}

getMediaContentSync10+

getMediaContentSync(resId: number, density?: number): Uint8Array

用户获取指定资源ID对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
Uint8Array资源ID对应的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContentSync($r('app.media.test').id); // 默认屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`);
}

try {
  this.context.resourceManager.getMediaContentSync($r('app.media.test').id, 120); // 指定屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`);
}

getMediaContentSync10+

getMediaContentSync(resource: Resource, density?: number): Uint8Array

用户获取指定resource对象对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
Uint8Arrayresource对象对应的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContentSync(resource); // 默认屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`);
}

try {
  this.context.resourceManager.getMediaContentSync(resource, 120); // 指定屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`);
}

getMediaByNameSync10+

getMediaByNameSync(resName: string, density?: number): Uint8Array

用户获取指定资源名称对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
Uint8Array对应资源名称的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaByNameSync("test"); // 默认屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`);
}

try {
  this.context.resourceManager.getMediaByNameSync("test", 120); // 指定屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`);
}

getMediaContent9+

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

用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`);
}

getMediaContent10+

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

用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error, value) => {
    if (error != null) {
      console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`);
}

getMediaContent9+

getMediaContent(resId: number): Promise<Uint8Array>

用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<Uint8Array>资源ID值对应的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContent($r('app.media.test').id).then((value: Uint8Array) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.log("getMediaContent promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`);
}

getMediaContent10+

getMediaContent(resId: number, density: number): Promise<Uint8Array>

用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度

返回值:

类型说明
Promise<Uint8Array>资源ID值对应的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then((value: Uint8Array) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`);
}

getMediaContent9+

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

用户获取指定resource对象对应的媒体文件内容,使用callback形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContent(resource, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`);
}

getMediaContent10+

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

用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContent(resource, 120, (error, value) => {
    if (error != null) {
      console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`);
}

getMediaContent9+

getMediaContent(resource: Resource): Promise<Uint8Array>

用户获取指定resource对象对应的媒体文件内容,使用Promise形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
Promise<Uint8Array>resource对象对应的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContent(resource).then((value: Uint8Array) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.log("getMediaContent promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`);
}

getMediaContent10+

getMediaContent(resource: Resource, density: number): Promise<Uint8Array>

用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度

返回值:

类型说明
Promise<Uint8Array>resource对象对应的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContent(resource, 120).then((value: Uint8Array) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`);
}

getMediaByName9+

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

用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaByName("test", (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`);
}

getMediaByName10+

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

用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaByName("test", 120, (error, value) => {
    if (error != null) {
      console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`);
}

getMediaByName9+

getMediaByName(resName: string): Promise<Uint8Array>

用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
Promise<Uint8Array>资源名称对应的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaByName("test").then((value: Uint8Array) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.log("getMediaByName promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`);
}

getMediaByName10+

getMediaByName(resName: string, density: number): Promise<Uint8Array>

用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度

返回值:

类型说明
Promise<Uint8Array>资源名称对应的媒体文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaByName("test", 120).then((value: Uint8Array) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase64Sync10+

getMediaContentBase64Sync(resId: number, density?: number): string

用户获取指定资源ID对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
string资源ID对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id); // 默认屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`);
}

try {
  this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id, 120); // 指定屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase64Sync10+

getMediaContentBase64Sync(resource: Resource, density?: number): string

用户获取指定resource对象对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
stringresource对象对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContentBase64Sync(resource); // 默认屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`);
}

try {
  this.context.resourceManager.getMediaContentBase64Sync(resource, 120); // 指定屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`);
}

getMediaBase64ByNameSync10+

getMediaBase64ByNameSync(resName: string, density?: number): string

用户获取指定资源名称对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
string资源名称对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaBase64ByNameSync("test"); // 默认屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`);
}

try {
  this.context.resourceManager.getMediaBase64ByNameSync("test", 120); // 指定屏幕密度
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase649+

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

用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数: Content

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<string>异步回调,用于返回获取的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase6410+

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

用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度
callbackAsyncCallback<string>异步回调,用于返回获取的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => {
    if (error != null) {
      console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase649+

getMediaContentBase64(resId: number): Promise<string>

用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<string>资源ID值对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then((value: string) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.log("getMediaContentBase64 promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase6410+

getMediaContentBase64(resId: number, density: number): Promise<string>

用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度

返回值:

类型说明
Promise<string>资源ID值对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then((value: string) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase649+

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

用户获取指定resource对象对应的图片资源Base64编码,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
callbackAsyncCallback<string>异步回调,用于返回获取的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContentBase64(resource, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase6410+

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

用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度
callbackAsyncCallback<string>异步回调,用于返回获取的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContentBase64(resource, 120, (error, value) => {
    if (error != null) {
      console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase649+

getMediaContentBase64(resource: Resource): Promise<string>

用户获取指定resource对象对应的图片资源Base64编码,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
Promise<string>resource对象对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContentBase64(resource).then((value: string) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.log("getMediaContentBase64 promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}

getMediaContentBase6410+

getMediaContentBase64(resource: Resource, density: number): Promise<string>

用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度

返回值:

类型说明
Promise<string>resource对象对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.test').id
};
try {
  this.context.resourceManager.getMediaContentBase64(resource, 120).then((value: string) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}

getMediaBase64ByName9+

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

用户获取指定资源名称对应的图片资源Base64编码,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
callbackAsyncCallback<string>异步回调,用于返回获取的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaBase64ByName("test", (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`);
}

getMediaBase64ByName10+

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

用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度
callbackAsyncCallback<string>异步回调,用于返回获取的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaBase64ByName("test", 120, (error, value) => {
    if (error != null) {
      console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
    } else {
      let media = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`);
}

getMediaBase64ByName9+

getMediaBase64ByName(resName: string): Promise<string>

用户获取指定资源名称对应的图片资源Base64编码,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
Promise<string>资源名称对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaBase64ByName("test").then((value: string) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.log("getMediaBase64ByName promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`);
}

getMediaBase64ByName10+

getMediaBase64ByName(resName: string, density: number): Promise<string>

用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
densitynumber资源获取需要的屏幕密度,0表示默认屏幕密度

返回值:

类型说明
Promise<string>资源名称对应的图片资源Base64编码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getMediaBase64ByName("test", 120).then((value: string) => {
    let media = value;
  }).catch((error: BusinessError) => {
    console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`);
}

getDrawableDescriptor10+

getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;

用户获取指定资源ID对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
DrawableDescriptor资源ID值对应的DrawableDescriptor对象

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}
try {
  this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}

getDrawableDescriptor10+

getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;

用户获取指定resource对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
DrawableDescriptor资源ID值对应的DrawableDescriptor对象

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.media.icon').id
};
try {
  this.context.resourceManager.getDrawableDescriptor(resource);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}
try {
  this.context.resourceManager.getDrawableDescriptor(resource, 120);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}

getDrawableDescriptorByName10+

getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor;

用户获取指定资源名称对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
densitynumber资源获取需要的屏幕密度,0或缺省表示默认屏幕密度

返回值:

类型说明
DrawableDescriptor资源ID值对应的DrawableDescriptor对象

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getDrawableDescriptorByName('icon');
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`);
}
try {
  this.context.resourceManager.getDrawableDescriptorByName('icon', 120);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`);
}

getBoolean9+

getBoolean(resId: number): boolean

使用同步方式,返回获取指定资源ID对应的布尔结果。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
boolean资源ID值对应的布尔结果

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getBoolean failed, error code: ${code}, message: ${message}.`);
}

getBoolean9+

getBoolean(resource: Resource): boolean

使用同步方式,返回获取指定resource对象对应的布尔结果。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
booleanresource对象对应的布尔结果

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.boolean.boolean_test').id
};
try {
  this.context.resourceManager.getBoolean(resource);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getBoolean failed, error code: ${code}, message: ${message}.`);
}

getBooleanByName9+

getBooleanByName(resName: string): boolean

使用同步方式,返回获取指定资源名称对应的布尔结果

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
boolean资源名称对应的布尔结果

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getBooleanByName("boolean_test");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getBooleanByName failed, error code: ${code}, message: ${message}.`);
}

getNumber9+

getNumber(resId: number): number

用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回资源对应的数值。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
number资源ID值对应的数值。Integer对应的是原数值,float对应的是真实像素点值,具体参考示例代码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer对应返回的是原数值
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getNumber failed, error code: ${code}, message: ${message}.`);
}

try {
  this.context.resourceManager.getNumber($r('app.float.float_test').id); // float对应返回的是真实像素点值
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getNumber failed, error code: ${code}, message: ${message}.`);
}

getNumber9+

getNumber(resource: Resource): number

用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回资源对应的数值。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
numberresource对象对应的数值。Integer对应的是原数值,float对应的是真实像素点值, 具体参考示例代码

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.integer.integer_test').id
};
try {
  this.context.resourceManager.getNumber(resource);// integer对应返回的是原数值, float对应返回的是真实像素点值
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getNumber failed, error code: ${code}, message: ${message}.`);
}

getNumberByName9+

getNumberByName(resName: string): number

用户获取指定资源名称对应的integer数值或者float数值,使用同步方式资源对应的数值。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
number资源名称对应的数值

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getNumberByName("integer_test");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`);
}

try {
  this.context.resourceManager.getNumberByName("float_test");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`);
}

getColorSync10+

getColorSync(resId: number) : number;

用户获取指定资源ID对应的颜色值,使用同步方式返回其对应的颜色值。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
number资源ID值对应的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getColorSync($r('app.color.test').id);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getColorSync failed, error code: ${code}, message: ${message}.`);
}

getColorSync10+

getColorSync(resource: Resource): number

用户获取指定resource对象对应的颜色值,使用同步方式返回其对应的颜色值。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
numberresource对象对应的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.color.test').id
};
try {
  this.context.resourceManager.getColorSync(resource);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getColorSync failed, error code: ${code}, message: ${message}.`);
}

getColorByNameSync10+

getColorByNameSync(resName: string) : number;

用户获取指定资源名称对应的颜色值,使用同步方式返回其对应的颜色值。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
number资源名称对应的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getColorByNameSync("test");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getColorByNameSync failed, error code: ${code}, message: ${message}.`);
}

getColor10+

getColor(resId: number, callback: AsyncCallback<number>): void;

用户获取指定资源ID对应的颜色值,使用callback形式返回其对应的颜色值。

系统能力: SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<number>异步回调,用于返回获取的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the module resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例Stage:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getColor($r('app.color.test').id, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getColor failed, error code: ${code}, message: ${message}.`);
}

getColor10+

getColor(resId: number): Promise<number>

用户获取指定资源ID对应的颜色值,使用Promise形式返回对应其对应的颜色值。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<number>资源ID值对应的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getColor($r('app.color.test').id).then((value: number) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("getColor promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getColor failed, error code: ${code}, message: ${message}.`);
}

getColor10+

getColor(resource: Resource, callback: AsyncCallback<number>): void;

用户获取指定resource对象对应的颜色值,使用callback形式返回其对应的颜色值。

系统能力: SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息
callbackAsyncCallback<number>异步回调,用于返回获取的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.color.test').id
};
try {
  this.context.resourceManager.getColor(resource, (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let str = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getColor failed, error code: ${code}, message: ${message}.`);
}

getColor10+

getColor(resource: Resource): Promise<number>;

用户获取指定resource对象对应的颜色值,使用Promise形式返回其对应的颜色值。

系统能力:SystemCapability.Global.ResourceManager

模型约束:此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
resourceResource资源信息

返回值:

类型说明
Promise<number>resource对象对应的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001001If the resId invalid.
9001002If the resource not found by resId.
9001006If the resource re-ref too much.

示例:

import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

let resource: resourceManager.Resource = {
  bundleName: "com.example.myapplication",
  moduleName: "entry",
  id: $r('app.color.test').id
};
try {
  this.context.resourceManager.getColor(resource).then((value: number) => {
    let str = value;
  }).catch((error: BusinessError) => {
    console.log("getColor promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getColor failed, error code: ${code}, message: ${message}.`);
}

getColorByName10+

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

用户获取指定资源名称对应的颜色值,使用callback形式返回其对应的颜色值。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称
callbackAsyncCallback<number>异步回调,用于返回获取的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getColorByName("test", (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let string = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getColorByName failed, error code: ${code}, message: ${message}.`);
}

getColorByName10+

getColorByName(resName: string): Promise<number>

用户获取指定资源名称对应的颜色值,使用Promise形式返回其对应的颜色值。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resNamestring资源名称

返回值:

类型说明
Promise<number>资源名称对应的颜色值(十进制)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001003If the resName invalid.
9001004If the resource not found by resName.
9001006If the resource re-ref too much.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getColorByName("test").then((value: number) => {
    let string = value;
  }).catch((error: BusinessError) => {
    console.log("getColorByName promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getColorByName failed, error code: ${code}, message: ${message}.`);
}

getRawFileContentSync10+

getRawFileContentSync(path: string): Uint8Array

用户获取resources/rawfile目录下对应的rawfile文件内容,使用同步形式返回字节数组。

系统能力: SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

返回值:

类型说明
Uint8Array返回获取的rawfile文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getRawFileContentSync("test.txt");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getRawFileContentSync failed, error code: ${code}, message: ${message}.`);
}

getRawFileContent9+

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

用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的rawfile文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getRawFileContent("test.txt", (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    } else {
      let rawFile = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`);
}

getRawFileContent9+

getRawFileContent(path: string): Promise<Uint8Array>

用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

返回值:

类型说明
Promise<Uint8Array>rawfile文件内容

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getRawFileContent("test.txt").then((value: Uint8Array) => {
    let rawFile = value;
  }).catch((error: BusinessError) => {
    console.log("getRawFileContent promise error is " + error);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getRawFileContent failed, error code: ${code}, message: ${message}.`);
}

getRawFileListSync10+

getRawFileListSync(path: string): Array<string>

用户获取resources/rawfile目录下文件夹及文件列表,使用同步形式返回文件列表的字符串数组。

系统能力: SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件夹路径

返回值:

类型说明
Array<string>rawfile文件夹下的列表(包含子文件夹和文件)

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try { // 传入""表示获取rawfile根目录下的文件列表
  this.context.resourceManager.getRawFileListSync("")
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getRawFileListSync failed, error code: ${code}, message: ${message}.`);
}

getRawFileList10+

getRawFileList(path: string, callback: AsyncCallback<Array<string>>): void;

用户获取resources/rawfile目录下文件夹及文件列表,使用callback形式返回文件列表的字符串数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件夹路径
callbackAsyncCallback<Array<string>>异步回调,用于返回获取rawfile文件目录下的文件列表

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try { // 传入""表示获取rawfile根目录下的文件列表
  this.context.resourceManager.getRawFileList("", (error, value) => {
    if (error != null) {
      console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
    } else {
      let rawFile = value;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getRawFileList failed, error code: ${code}, message: ${message}.`);
}

getRawFileList10+

getRawFileList(path: string): Promise<Array<string>>

用户获取resources/rawfile目录下文件夹及文件列表,使用Promise形式返回文件列表字符串数组。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件夹路径

返回值:

类型说明
Promise<Array<string>>rawfile文件目录下的文件列表

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try { // 传入""表示获取rawfile根目录下的文件列表
  this.context.resourceManager.getRawFileList("").then((value: Array<string>) => {
    let rawFile = value;
  }).catch((error: BusinessError) => {
    console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getRawFileList failed, error code: ${code}, message: ${message}.`);
}

getRawFdSync10+

getRawFdSync(path: string): RawFileDescriptor

用户获取resources/rawfile目录下对应rawfile文件的descriptor。

系统能力: SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

返回值:

类型说明
RawFileDescriptorrawfile文件的descriptor

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getRawFdSync("test.txt");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`getRawFdSync failed, error code: ${code}, message: ${message}.`);
}

getRawFd9+

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

用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径
callbackAsyncCallback<RawFileDescriptor>异步回调,用于返回获取的rawfile文件的descriptor

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getRawFd("test.txt", (error, value) => {
    if (error != null) {
      console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`);
    } else {
      let fd = value.fd;
      let offset = value.offset;
      let length = value.length;
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getRawFd failed, error code: ${code}, message: ${message}.`);
}

getRawFd9+

getRawFd(path: string): Promise<RawFileDescriptor>

用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

返回值:

类型说明
Promise<RawFileDescriptor>rawfile文件descriptor

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getRawFd("test.txt").then((value: resourceManager.RawFileDescriptor) => {
    let fd = value.fd;
    let offset = value.offset;
    let length = value.length;
  }).catch((error: BusinessError) => {
    console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise getRawFd failed, error code: ${code}, message: ${message}.`);
}

closeRawFdSync10+

closeRawFdSync(path: string): void

用户关闭resources/rawfile目录下rawfile文件的descriptor。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005The resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.closeRawFdSync("test.txt");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`closeRawFd failed, error code: ${code}, message: ${message}.`);
}

closeRawFd9+

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

用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径
callbackAsyncCallback<void>异步回调

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005The resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.closeRawFd("test.txt", (error, value) => {
    if (error != null) {
      console.log("error is " + error);
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback closeRawFd failed, error code: ${code}, message: ${message}.`);
}

closeRawFd9+

closeRawFd(path: string): Promise<void>

用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

返回值:

类型说明
Promise<void>无返回值

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001005If the resource not found by path.

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.closeRawFd("test.txt");
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`promise closeRawFd failed, error code: ${code}, message: ${message}.`);
}

getConfigurationSync

getConfigurationSync(): Configuration

用户获取设备的Configuration,使用同步形式返回Configuration对象。

系统能力:SystemCapability.Global.ResourceManager

返回值:

类型说明
Configuration设备的Configuration

示例:

try {
  let value = this.context.resourceManager.getConfigurationSync();
  let direction = value.direction;
  let locale = value.locale;
} catch (error) {
  console.error("getConfigurationSync error is " + error);
}

getConfiguration

getConfiguration(callback: AsyncCallback<Configuration>): void

用户获取设备的Configuration,使用callback形式返回Configuration对象。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
callbackAsyncCallback<Configuration>异步回调,用于返回设备的Configuration

示例:

try {
  this.context.resourceManager.getConfiguration((error, value) => {
    if (error != null) {
      console.error("getConfiguration callback error is " + error);
    } else {
      let direction = value.direction;
      let locale = value.locale;
    }
  });
} catch (error) {
  console.error("getConfiguration callback error is " + error);
}

getConfiguration

getConfiguration(): Promise<Configuration>

用户获取设备的Configuration,使用Promise形式返回Configuration对象。

系统能力:SystemCapability.Global.ResourceManager

返回值:

类型说明
Promise<Configuration>设备的Configuration

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getConfiguration().then((value: resourceManager.Configuration) => {
    let direction = value.direction;
    let locale = value.locale;
  }).catch((error: BusinessError) => {
    console.error("getConfiguration promise error is " + error);
  });
} catch (error) {
  console.error("getConfiguration promise error is " + error);
}

getDeviceCapabilitySync

getDeviceCapabilitySync(): DeviceCapability

用户获取设备的DeviceCapability,使用同步形式返回DeviceCapability对象。

系统能力:SystemCapability.Global.ResourceManager

返回值:

类型说明
DeviceCapability设备的DeviceCapability

示例:

try {
  let value = this.context.resourceManager.getDeviceCapabilitySync();
  let screenDensity = value.screenDensity;
  let deviceType = value.deviceType;
} catch (error) {
  console.error("getDeviceCapabilitySync error is " + error);
}

getDeviceCapability

getDeviceCapability(callback: AsyncCallback<DeviceCapability>): void

用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
callbackAsyncCallback<DeviceCapability>异步回调,用于返回设备的DeviceCapability

示例:

try {
  this.context.resourceManager.getDeviceCapability((error, value) => {
    if (error != null) {
      console.error("getDeviceCapability callback error is " + error);
    } else {
      let screenDensity = value.screenDensity;
      let deviceType = value.deviceType;
    }
  });
} catch (error) {
  console.error("getDeviceCapability callback error is " + error);
}

getDeviceCapability

getDeviceCapability(): Promise<DeviceCapability>

用户获取设备的DeviceCapability,使用Promise形式返回DeviceCapability对象。

系统能力:SystemCapability.Global.ResourceManager

返回值:

类型说明
Promise<DeviceCapability>设备的DeviceCapability

示例:

import { BusinessError } from '@ohos.base';

try {
  this.context.resourceManager.getDeviceCapability().then((value: resourceManager.DeviceCapability) => {
    let screenDensity = value.screenDensity;
    let deviceType = value.deviceType;
  }).catch((error: BusinessError) => {
    console.error("getDeviceCapability promise error is " + error);
  });
} catch (error) {
  console.error("getDeviceCapability promise error is " + error);
}

release7+

release()

用户释放创建的resourceManager, 此接口暂不支持。

系统能力:SystemCapability.Global.ResourceManager

示例:

try {
  this.context.resourceManager.release();
} catch (error) {
  console.error("release error is " + error);
}

addResource10+

addResource(path: string) : void;

应用运行时,加载指定的资源路径,实现资源覆盖。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstring资源路径

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001010If the overlay path is invalid.

示例:

import { BusinessError } from '@ohos.base';

let path = getContext().bundleCodeDir + "/library1-default-signed.hsp";
try {
  this.context.resourceManager.addResource(path);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`addResource failed, error code: ${code}, message: ${message}.`);
}

removeResource10+

removeResource(path: string) : void;

用户运行时,移除指定的资源路径,还原被覆盖前的资源。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstring资源路径

错误码:

以下错误码的详细介绍请参见资源管理错误码

错误码ID错误信息
9001010If the overlay path is invalid.

示例:

import { BusinessError } from '@ohos.base';

let path = getContext().bundleCodeDir + "/library1-default-signed.hsp";
try {
  this.context.resourceManager.removeResource(path);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`removeResource failed, error code: ${code}, message: ${message}.`);
}

getString(deprecated)

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

用户获取指定资源ID对应的字符串,使用callback形式返回字符串。

从API version 9开始不再维护,建议使用getStringValue代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<string>异步回调,用于返回获取的字符串

示例:

resourceManager.getResourceManager((error, mgr) => {
    mgr.getString($r('app.string.test').id, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let str = value;
        }
    });
});

getString(deprecated)

getString(resId: number): Promise<string>

用户获取指定资源ID对应的字符串,使用Promise形式返回字符串。

从API version 9开始不再维护,建议使用getStringValue代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<string>资源ID值对应的字符串

示例:

import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager((error, mgr) => {
    mgr.getString($r('app.string.test').id).then((value: string) => {
        let str = value;
    }).catch((error: BusinessError) => {
        console.log("getstring promise error is " + error);
    });
});

getStringArray(deprecated)

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

用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组。

从API version 9开始不再维护,建议使用getStringArrayValue代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<Array<string>>异步回调,用于返回获取的字符串数组

示例:

resourceManager.getResourceManager((error, mgr) => {
    mgr.getStringArray($r('app.strarray.test').id, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let strArray = value;
        }
    });
});

getStringArray(deprecated)

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

用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组。

从API version 9开始不再维护,建议使用getStringArrayValue代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<Array<string>>资源ID值对应的字符串数组

示例:

import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager((error, mgr) => {
     mgr.getStringArray($r('app.strarray.test').id).then((value: Array<string>) => {
        let strArray = value;
    }).catch((error: BusinessError) => {
        console.log("getStringArray promise error is " + error);
    });
});

getMedia(deprecated)

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

用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。

从API version 9开始不再维护,建议使用getMediaContent代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的媒体文件内容

示例:

resourceManager.getResourceManager((error, mgr) => {
    mgr.getMedia($r('app.media.test').id, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let media = value;
        }
    });
});

getMedia(deprecated)

getMedia(resId: number): Promise<Uint8Array>

用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组。

从API version 9开始不再维护,建议使用getMediaContent代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<Uint8Array>资源ID值对应的媒体文件内容

示例:

import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager((error, mgr) => {
    mgr.getMedia($r('app.media.test').id).then((value: Uint8Array) => {
        let media = value;
    }).catch((error: BusinessError) => {
        console.log("getMedia promise error is " + error);
    });
});

getMediaBase64(deprecated)

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

用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串。

从API version 9开始不再维护,建议使用getMediaContentBase64代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
callbackAsyncCallback<string>异步回调,用于返回获取的图片资源Base64编码

示例:

resourceManager.getResourceManager((error, mgr) => {
    mgr.getMediaBase64($r('app.media.test').id, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let media = value;
        }
    });
});

getMediaBase64(deprecated)

getMediaBase64(resId: number): Promise<string>

用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串。

从API version 9开始不再维护,建议使用getMediaContentBase64代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值

返回值:

类型说明
Promise<string>资源ID值对应的图片资源Base64编码

示例:

import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager((error, mgr) => {
    mgr.getMediaBase64($r('app.media.test').id).then((value: string) => {
        let media = value;
    }).catch((error: BusinessError) => {
        console.log("getMediaBase64 promise error is " + error);
    });
});

getPluralString(deprecated)

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

根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串。

从API version 9开始不再维护,建议使用getPluralStringValue代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
numnumber数量值

返回值:

类型说明
Promise<string>根据提供的数量获取对应ID字符串表示的单复数字符串

示例:

import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager((error, mgr) => {
    mgr.getPluralString($r("app.plural.test").id, 1).then((value: string) => {
        let str = value;
    }).catch((error: BusinessError) => {
        console.log("getPluralString promise error is " + error);
    });
});

getPluralString(deprecated)

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

根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。

从API version 9开始不再维护,建议使用getPluralStringValue代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
resIdnumber资源ID值
numnumber数量值
callbackAsyncCallback<string>异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串

示例:

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

getRawFile(deprecated)

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

用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。

从API version 9开始不再维护,建议使用getRawFileContent代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径
callbackAsyncCallback<Uint8Array>异步回调,用于返回获取的rawfile文件内容

示例:

resourceManager.getResourceManager((error, mgr) => {
    mgr.getRawFile("test.txt", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let rawFile = value;
        }
    });
});

getRawFile(deprecated)

getRawFile(path: string): Promise<Uint8Array>

用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。

从API version 9开始不再维护,建议使用getRawFileContent代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

返回值:

类型说明
Promise<Uint8Array>rawfile文件内容

示例:

import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager((error, mgr) => {
    mgr.getRawFile("test.txt").then((value: Uint8Array) => {
        let rawFile = value;
    }).catch((error: BusinessError) => {
        console.log("getRawFile promise error is " + error);
    });
});

getRawFileDescriptor(deprecated)

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

用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回。

从API version 9开始不再维护,建议使用getRawFd代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径
callbackAsyncCallback<RawFileDescriptor>异步回调,用于返回获取的rawfile文件的descriptor

示例:

resourceManager.getResourceManager((error, mgr) => {
    mgr.getRawFileDescriptor("test.txt", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let fd = value.fd;
            let offset = value.offset;
            let length = value.length;
        }
    });
});

getRawFileDescriptor(deprecated)

getRawFileDescriptor(path: string): Promise<RawFileDescriptor>

用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回。

从API version 9开始不再维护,建议使用getRawFd代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

返回值:

类型说明
Promise<RawFileDescriptor>rawfile文件descriptor

示例:

import { BusinessError } from '@ohos.base';

resourceManager.getResourceManager((error, mgr) => {
    mgr.getRawFileDescriptor("test.txt").then((value: resourceManager.RawFileDescriptor) => {
        let fd = value.fd;
        let offset = value.offset;
        let length = value.length;
    }).catch((error: BusinessError) => {
        console.log("getRawFileDescriptor promise error is " + error);
    });
});

closeRawFileDescriptor(deprecated)

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

用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回。

从API version 9开始不再维护,建议使用closeRawFd代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径
callbackAsyncCallback<void>异步回调

示例:

resourceManager.getResourceManager((error, mgr) => {
    mgr.closeRawFileDescriptor("test.txt", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        }
    });
});

closeRawFileDescriptor(deprecated)

closeRawFileDescriptor(path: string): Promise<void>

用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回。

从API version 9开始不再维护,建议使用closeRawFd代替。

系统能力:SystemCapability.Global.ResourceManager

参数:

参数名类型必填说明
pathstringrawfile文件路径

返回值:

类型说明
Promise<void>无返回值

示例:

resourceManager.getResourceManager((error, mgr) => {
    mgr.closeRawFileDescriptor("test.txt");
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/9464a1a77da94b2bbc785a30e88c217f