提交 c612816a 编写于 作者: F fangyunzhong

资源管理模块arkts整改

Signed-off-by: Nfangyunzhong <fangyunzhong2@huawei.com>
上级 b5aedf06
...@@ -22,10 +22,10 @@ Stage模型下Context的引用方法请参考[Stage模型的Context详细介绍] ...@@ -22,10 +22,10 @@ Stage模型下Context的引用方法请参考[Stage模型的Context详细介绍]
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
let context = this.context; let context = this.context;
let resourceManager = context.resourceManager; let resourceManager = context.resourceManager;
} }
} }
``` ```
...@@ -48,17 +48,17 @@ getResourceManager(callback: AsyncCallback&lt;ResourceManager&gt;): void ...@@ -48,17 +48,17 @@ getResourceManager(callback: AsyncCallback&lt;ResourceManager&gt;): void
**示例:** **示例:**
```js ```js
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
if (error != null) {
console.log("error is " + error);
return;
}
mgr.getStringValue(0x1000000, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
return; } else {
let str = value;
} }
mgr.getStringValue(0x1000000, (error, value) => { });
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
}); });
``` ```
注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。 注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。
...@@ -106,16 +106,19 @@ getResourceManager(): Promise&lt;ResourceManager&gt; ...@@ -106,16 +106,19 @@ getResourceManager(): Promise&lt;ResourceManager&gt;
**示例:** **示例:**
```js ```js
resourceManager.getResourceManager().then(mgr => { import resourceManager from '@ohos.resourceManager';
mgr.getStringValue(0x1000000, (error, value) => { import { BusinessError } from '@ohos.base';
if (error != null) {
console.log("error is " + error); resourceManager.getResourceManager().then((mgr: resourceManager.ResourceManager) => {
} else { mgr.getStringValue(0x1000000, (error, value) => {
let str = value; if (error != null) {
} console.log("error is " + error);
}); } else {
}).catch(error => { let str = value;
console.log("error is " + error); }
});
}).catch((error: BusinessError) => {
console.log("error is " + error);
}); });
``` ```
注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。 注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。
...@@ -145,12 +148,14 @@ getResourceManager(bundleName: string): Promise&lt;ResourceManager&gt; ...@@ -145,12 +148,14 @@ getResourceManager(bundleName: string): Promise&lt;ResourceManager&gt;
**示例:** **示例:**
```js ```js
resourceManager.getResourceManager("com.example.myapplication").then(mgr => { import resourceManager from '@ohos.resourceManager';
}).catch(error => { import { BusinessError } from '@ohos.base';
resourceManager.getResourceManager("com.example.myapplication").then((mgr: resourceManager.ResourceManager) => {
}).catch((error: BusinessError) => {
}); });
``` ```
## resourceManager.getSystemResourceManager<sup>10+</sup> ## resourceManager.getSystemResourceManager<sup>10+</sup>
getSystemResourceManager(): ResourceManager getSystemResourceManager(): ResourceManager
...@@ -176,17 +181,18 @@ getSystemResourceManager(): ResourceManager ...@@ -176,17 +181,18 @@ getSystemResourceManager(): ResourceManager
**示例:** **示例:**
```js ```js
import resourceManager from '@ohos.resourceManager'; import resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';
try { try {
let systemResourceManager = resourceManager.getSystemResourceManager(); let systemResourceManager = resourceManager.getSystemResourceManager();
systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then(value => { systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then((value: string) => {
let str = value; let str = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("systemResourceManager getStringValue promise error is " + error); console.log("systemResourceManager getStringValue promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`systemResourceManager getStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`systemResourceManager getStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
...@@ -301,20 +307,25 @@ try { ...@@ -301,20 +307,25 @@ try {
> >
> - 资源文件在工程的resources目录中定义,id可通过$r(资源地址).id的方式获取,例如$r('app.string.test').id。 > - 资源文件在工程的resources目录中定义,id可通过$r(资源地址).id的方式获取,例如$r('app.string.test').id。
### getStringValue<sup>9+</sup> ### getStringSync<sup>9+</sup>
getStringValue(resId: number, callback: AsyncCallback&lt;string&gt;): void getStringSync(resId: number): string
用户获取指定资源ID对应的字符串,使用callback形式返回字符串。 用户获取指定资源ID对应的字符串,使用同步方式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- | | ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| string | 资源ID值对应的字符串 |
**错误码:** **错误码:**
...@@ -322,31 +333,24 @@ getStringValue(resId: number, callback: AsyncCallback&lt;string&gt;): void ...@@ -322,31 +333,24 @@ getStringValue(resId: number, callback: AsyncCallback&lt;string&gt;): void
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the module resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例Stage:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => { this.context.resourceManager.getStringSync($r('app.string.test').id);
if (error != null) { } catch (error) {
console.log("error is " + error); console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
} else { }
let str = value;
}
});
} catch (error) {
console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`);
}
``` ```
### getStringSync<sup>10+</sup>
### getStringValue<sup>9+</sup> getStringSync(resId: number, ...args: Array<string | number>): string
getStringValue(resId: number): Promise&lt;string&gt;
用户获取指定资源ID对应的字符串,使用Promise形式返回字符串。 用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -355,42 +359,39 @@ getStringValue(resId: number): Promise&lt;string&gt; ...@@ -355,42 +359,39 @@ getStringValue(resId: number): Promise&lt;string&gt;
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> %d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ----------- | | ------ | ---------------------------- |
| Promise&lt;string&gt; | 资源ID值对应的字符串 | | string | 资源ID值对应的格式化字符串|
**错误码:** **错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md) 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ----------------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
| 9001007 | If the resource obtained by resId formatting error. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringValue($r('app.string.test').id).then(value => { this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78);
let str = value;
}).catch(error => {
console.log("getStringValue promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>9+</sup>
### getStringValue<sup>9+</sup> getStringSync(resource: Resource): string
getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
用户获取指定resource对象对应的字符串,使用callback形式返回字符串。 用户获取指定resource对象对应的字符串,使用同步方式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -398,10 +399,15 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void ...@@ -398,10 +399,15 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- | | -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------------- |
| string | resource对象对应的字符串 |
**错误码:** **错误码:**
...@@ -415,47 +421,42 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void ...@@ -415,47 +421,42 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication",
moduleName: "entry", let resource: resourceManager.Resource = {
id: $r('app.string.test').id bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
}; };
try { try {
this.context.resourceManager.getStringValue(resource, (error, value) => { this.context.resourceManager.getStringSync(resource);
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) { } catch (error) {
console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>10+</sup>
### getStringValue<sup>9+</sup> getStringSync(resource: Resource, ...args: Array<string | number>): string
getStringValue(resource: Resource): Promise&lt;string&gt;
用户获取指定resource对象对应的字符串,使用Promise形式返回字符串。 用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> %d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ---------------- | | ------ | ---------------------------- |
| Promise&lt;string&gt; | resource对象对应的字符串 | | string | resource对象对应的格式化字符串|
**错误码:** **错误码:**
...@@ -466,40 +467,43 @@ getStringValue(resource: Resource): Promise&lt;string&gt; ...@@ -466,40 +467,43 @@ getStringValue(resource: Resource): Promise&lt;string&gt;
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
| 9001007 | If the resource obtained by resId formatting error. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication",
moduleName: "entry", let resource: resourceManager.Resource = {
id: $r('app.string.test').id bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
}; };
try { try {
this.context.resourceManager.getStringValue(resource).then(value => { this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78);
let str = value;
}).catch(error => {
console.log("getStringValue promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringArrayValue<sup>9+</sup> ### getStringByNameSync<sup>9+</sup>
getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void getStringByNameSync(resName: string): string
用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组 用户获取指定资源名称对应的字符串,使用同步方式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| string | 资源名称对应的字符串 |
**错误码:** **错误码:**
...@@ -507,45 +511,39 @@ getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt ...@@ -507,45 +511,39 @@ getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => { this.context.resourceManager.getStringByNameSync("test");
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
} catch (error) { } catch (error) {
console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringByNameSync<sup>10+</sup>
### getStringArrayValue<sup>9+</sup> getStringByNameSync(resName: string, ...args: Array<string | number>): string
getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt;
用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组 用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> %d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ---------------------------------- | ------------- | | ------ | ---------------------------- |
| Promise&lt;Array&lt;string&gt;&gt; | 资源ID值对应的字符串数组 | | string | 资源名称对应的格式化字符串|
**错误码:** **错误码:**
...@@ -553,39 +551,34 @@ getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt; ...@@ -553,39 +551,34 @@ getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
| 9001008 | If the resource obtained by resName formatting error. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then(value => { this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78);
let strArray = value;
}).catch(error => {
console.log("getStringArrayValue promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringArrayValue<sup>9+</sup> ### getStringValue<sup>9+</sup>
getStringArrayValue(resource: Resource, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void getStringValue(resId: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定resource对象对应的字符串数组,使用callback形式返回回字符串数组 用户获取指定资源ID对应的字符串,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- | | -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**错误码:** **错误码:**
...@@ -593,51 +586,44 @@ getStringArrayValue(resource: Resource, callback: AsyncCallback&lt;Array&lt;stri ...@@ -593,51 +586,44 @@ getStringArrayValue(resource: Resource, callback: AsyncCallback&lt;Array&lt;stri
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the module resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例Stage:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
};
try { try {
this.context.resourceManager.getStringArrayValue(resource, (error, value) => { this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let strArray = value; let str = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringArrayValue<sup>9+</sup> ### getStringValue<sup>9+</sup>
getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt; getStringValue(resId: number): Promise&lt;string&gt;
用户获取指定resource对象对应的字符串数组,使用Promise形式返回字符串数组 用户获取指定资源ID对应的字符串,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resId | number | 是 | 资源ID值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ---------------------------------- | ------------------ | | --------------------- | ----------- |
| Promise&lt;Array&lt;string&gt;&gt; | resource对象对应的字符串数组 | | Promise&lt;string&gt; | 资源ID值对应的字符串 |
**错误码:** **错误码:**
...@@ -651,36 +637,35 @@ getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt; ...@@ -651,36 +637,35 @@ getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt;
**示例:** **示例:**
```ts ```ts
let resource = { import { BusinessError } from '@ohos.base';
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
};
try { try {
this.context.resourceManager.getStringArrayValue(resource).then(value => { this.context.resourceManager.getStringValue($r('app.string.test').id).then((value: string) => {
let strArray = value; let str = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getStringArray promise error is " + error); console.log("getStringValue promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>9+</sup> ### getStringValue<sup>9+</sup>
getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组 用户获取指定resource对象对应的字符串,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ | | -------- | --------------------------- | ---- | --------------- |
| resId | number | 是 | 资源ID值 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**错误码:** **错误码:**
...@@ -690,37 +675,51 @@ getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void ...@@ -690,37 +675,51 @@ getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import resourceManager from '@ohos.resourceManager';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
try { try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => { this.context.resourceManager.getStringValue(resource, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let media = value; let str = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>10+</sup> ### getStringValue<sup>9+</sup>
getMediaContent(resId: number, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void getStringValue(resource: Resource): Promise&lt;string&gt;
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组 用户获取指定resource对象对应的字符串,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
| 参数名 | 类型 | 必填 | 说明 | **参数:**
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | 是 | 资源ID值 | | 参数名 | 类型 | 必填 | 说明 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | -------- | ---------------------- | ---- | ---- |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 | | resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------- |
| Promise&lt;string&gt; | resource对象对应的字符串 |
**错误码:** **错误码:**
...@@ -730,41 +729,43 @@ getMediaContent(resId: number, density: number, callback: AsyncCallback&lt;Uint8 ...@@ -730,41 +729,43 @@ getMediaContent(resId: number, density: number, callback: AsyncCallback&lt;Uint8
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
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 { try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error, value) => { this.context.resourceManager.getStringValue(resource).then((value: string) => {
if (error != null) { let str = value;
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); }).catch((error: BusinessError) => {
} else { console.log("getStringValue promise error is " + error);
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>9+</sup> ### getStringByName<sup>9+</sup>
getMediaContent(resId: number): Promise&lt;Uint8Array&gt; getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组 用户获取指定资源名称对应的字符串,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------------------------- | -------------- | | -------- | --------------------------- | ---- | --------------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**错误码:** **错误码:**
...@@ -772,42 +773,44 @@ getMediaContent(resId: number): Promise&lt;Uint8Array&gt; ...@@ -772,42 +773,44 @@ getMediaContent(resId: number): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getMediaContent($r('app.media.test').id).then(value => { this.context.resourceManager.getStringByName("test", (error, value) => {
let media = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.log("getMediaContent promise error is " + error); } else {
}); let str = value;
}
});
} catch (error) { } catch (error) {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>10+</sup> ### getStringByName<sup>9+</sup>
getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt; getStringByName(resName: string): Promise&lt;string&gt;
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组 用户获取指定资源名称对应的字符串,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | -------------- | | --------------------- | ---------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 | | Promise&lt;string&gt; | 资源名称对应的字符串 |
**错误码:** **错误码:**
...@@ -815,38 +818,39 @@ getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt; ...@@ -815,38 +818,39 @@ getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then(value => { this.context.resourceManager.getStringByName("test").then((value: string) => {
let media = value; let str = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.log("getStringByName promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>9+</sup> ### getStringArrayValue<sup>9+</sup>
getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): void getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
用户获取指定resource对象对应的媒体文件内容,使用callback形式返回字节数组。 用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ | | -------- | ---------------------------------------- | ---- | ----------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 | | callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**错误码:** **错误码:**
...@@ -856,44 +860,42 @@ getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): ...@@ -856,44 +860,42 @@ getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;):
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaContent(resource, (error, value) => { this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let media = value; let strArray = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>10+</sup> ### getStringArrayValue<sup>9+</sup>
getMediaContent(resource: Resource, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt;
用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。 用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ | | ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 | **返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------- |
| Promise&lt;Array&lt;string&gt;&gt; | 资源ID值对应的字符串数组 |
**错误码:** **错误码:**
...@@ -903,32 +905,28 @@ getMediaContent(resource: Resource, density: number, callback: AsyncCallback&lt; ...@@ -903,32 +905,28 @@ getMediaContent(resource: Resource, density: number, callback: AsyncCallback&lt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import { BusinessError } from '@ohos.base';
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaContent(resource, 120, (error, value) => { this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then((value: Array<string>) => {
if (error != null) { let strArray = value;
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); }).catch((error: BusinessError) => {
} else { console.log("getStringArrayValue promise error is " + error);
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>9+</sup> ### getStringArrayValue<sup>9+</sup>
getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt; getStringArrayValue(resource: Resource, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
用户获取指定resource对象对应的媒体文件内容,使用Promise形式返回字节数组。 用户获取指定resource对象对应的字符串数组,使用callback形式返回回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -936,15 +934,10 @@ getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt; ...@@ -936,15 +934,10 @@ getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt;
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ---------------------------------------- | ---- | ----------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------------- |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -954,30 +947,35 @@ getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt; ...@@ -954,30 +947,35 @@ getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication",
moduleName: "entry", let resource: resourceManager.Resource = {
id: $r('app.media.test').id bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
}; };
try { try {
this.context.resourceManager.getMediaContent(resource).then(value => { this.context.resourceManager.getStringArrayValue(resource, (error, value) => {
let media = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.log("getMediaContent promise error is " + error); } else {
let strArray = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>10+</sup> ### getStringArrayValue<sup>9+</sup>
getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt; getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt;
用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。 用户获取指定resource对象对应的字符串数组,使用Promise形式返回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -988,13 +986,12 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt; ...@@ -988,13 +986,12 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt;
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | ------------------- | | ---------------------------------- | ------------------ |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 | | Promise&lt;Array&lt;string&gt;&gt; | resource对象对应的字符串数组 |
**错误码:** **错误码:**
...@@ -1004,39 +1001,43 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt; ...@@ -1004,39 +1001,43 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication", import { BusinessError } from '@ohos.base';
moduleName: "entry",
id: $r('app.media.test').id let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
}; };
try { try {
this.context.resourceManager.getMediaContent(resource, 120).then(value => { this.context.resourceManager.getStringArrayValue(resource).then((value: Array<string>) => {
let media = value; let strArray = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.log("getStringArray promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>9+</sup> ### getStringArrayByName<sup>9+</sup>
getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void getStringArrayByName(resName: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串 用户获取指定资源名称对应的字符串数组,使用callback形式返回字符串数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | -------- | ---------------------------------------- | ---- | ----------------- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 | | callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**错误码:** **错误码:**
...@@ -1044,39 +1045,44 @@ getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): voi ...@@ -1044,39 +1045,44 @@ getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): voi
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => { this.context.resourceManager.getStringArrayByName("test", (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let media = value; let strArray = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>10+</sup> ### getStringArrayByName<sup>9+</sup>
getMediaContentBase64(resId: number, density: number, callback: AsyncCallback&lt;string&gt;): void getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt;
用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串 用户获取指定资源名称对应的字符串数组,使用Promise形式返回字符串数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 | **返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------ |
| Promise&lt;Array&lt;string&gt;&gt; | 资源名称对应的字符串数组 |
**错误码:** **错误码:**
...@@ -1084,43 +1090,40 @@ getMediaContentBase64(resId: number, density: number, callback: AsyncCallback&lt ...@@ -1084,43 +1090,40 @@ getMediaContentBase64(resId: number, density: number, callback: AsyncCallback&lt
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => { this.context.resourceManager.getStringArrayByName("test").then((value: Array<string>) => {
if (error != null) { let strArray = value;
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); }).catch((error: BusinessError) => {
} else { console.log("getStringArrayByName promise error is " + error);
let media = value; });
}
});
} catch (error) { } catch (error) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>9+</sup> ### getPluralStringValue<sup>9+</sup>
getMediaContentBase64(resId: number): Promise&lt;string&gt; getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串。 根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | -------- | --------------------------- | ---- | ------------------------------- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 |
**返回值:** | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 |
| 类型 | 说明 |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -1130,25 +1133,28 @@ getMediaContentBase64(resId: number): Promise&lt;string&gt; ...@@ -1130,25 +1133,28 @@ getMediaContentBase64(resId: number): Promise&lt;string&gt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then(value => { this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => {
let media = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.log("getMediaContentBase64 promise error is " + error); } else {
let str = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>10+</sup> ### getPluralStringValue<sup>9+</sup>
getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt; getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。 根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -1157,13 +1163,13 @@ getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt; ...@@ -1157,13 +1163,13 @@ getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt;
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | num | number | 是 | 数量值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | -------------------- | | --------------------- | ------------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 | | Promise&lt;string&gt; | 根据提供的数量获取对应ID字符串表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1173,36 +1179,40 @@ getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt; ...@@ -1173,36 +1179,40 @@ getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then(value => { this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then((value: string) => {
let media = value; let str = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.log("getPluralStringValue promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>9+</sup> ### getPluralStringValue<sup>9+</sup>
getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;): void getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定resource对象对应的图片资源Base64编码,使用callback形式返回字符串。 根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | -------- | --------------------------- | ---- | ------------------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 | | num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1212,44 +1222,52 @@ getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;) ...@@ -1212,44 +1222,52 @@ getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;)
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication",
moduleName: "entry", let resource: resourceManager.Resource = {
id: $r('app.media.test').id bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.plural.test').id
}; };
try { try {
this.context.resourceManager.getMediaContentBase64(resource, (error, value) => { this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let media = value; let str = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>10+</sup> ### getPluralStringValue<sup>9+</sup>
getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallback&lt;string&gt;): void getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt;
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。 根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------------ |
| Promise&lt;string&gt; | 根据提供的数量获取对应resource对象表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1259,48 +1277,44 @@ getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallba ...@@ -1259,48 +1277,44 @@ getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallba
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication", import { BusinessError } from '@ohos.base';
moduleName: "entry",
id: $r('app.media.test').id let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.plural.test').id
}; };
try { try {
this.context.resourceManager.getMediaContentBase64(resource, 120, (error, value) => { this.context.resourceManager.getPluralStringValue(resource, 1).then((value: string) => {
if (error != null) { let str = value;
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); }).catch((error: BusinessError) => {
} else { console.log("getPluralStringValue promise error is " + error);
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>9+</sup> ### getPluralStringByName<sup>9+</sup>
getMediaContentBase64(resource: Resource): Promise&lt;string&gt; getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定resource对象对应的图片资源Base64编码,使用Promise形式返回字符串。 根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | --------------------------- | ---- | ----------------------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resName | string | 是 | 资源名称 |
| num | number | 是 | 数量值 |
**返回值:** | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据传入的数量值获取资源名称对应的字符串资源 |
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -1308,49 +1322,45 @@ getMediaContentBase64(resource: Resource): Promise&lt;string&gt; ...@@ -1308,49 +1322,45 @@ getMediaContentBase64(resource: Resource): Promise&lt;string&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaContentBase64(resource).then(value => { this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
let media = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.log("getMediaContentBase64 promise error is " + error); } else {
let str = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>10+</sup> ### getPluralStringByName<sup>9+</sup>
getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt; getPluralStringByName(resName: string, num: number): Promise&lt;string&gt;
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。 根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | ------- | ------ | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | num | number | 是 | 数量值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ------------------------- | | --------------------- | ---------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 | | Promise&lt;string&gt; | 根据传入的数量值获取资源名称对应的字符串资源 |
**错误码:** **错误码:**
...@@ -1358,161 +1368,168 @@ getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt ...@@ -1358,161 +1368,168 @@ getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import { BusinessError } from '@ohos.base';
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaContentBase64(resource, 120).then(value => { this.context.resourceManager.getPluralStringByName("test", 1).then((value: string) => {
let media = value; let str = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.log("getPluralStringByName promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getConfiguration ### getMediaContent<sup>9+</sup>
getConfiguration(callback: AsyncCallback&lt;Configuration&gt;): void getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取设备的Configuration,使用callback形式返回Configuration对象 用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ------------------------- | | -------- | ------------------------------- | ---- | ------------------ |
| callback | AsyncCallback&lt;[Configuration](#configuration)&gt; | 是 | 异步回调,用于返回设备的Configuration | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getConfiguration((error, value) => { this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => {
if (error != null) { if (error != null) {
console.error("getConfiguration callback error is " + error); console.log("error is " + error);
} else { } else {
let direction = value.direction; let media = value;
let locale = value.locale;
} }
}); });
} catch (error) { } catch (error) {
console.error("getConfiguration callback error is " + error); console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>10+</sup>
### getConfiguration getMediaContent(resId: number, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
getConfiguration(): Promise&lt;Configuration&gt;
用户获取设备的Configuration,使用Promise形式返回Configuration对象 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**返回值:** **参数:**
| 类型 | 说明 |
| ---------------------------------------- | ---------------- |
| Promise&lt;[Configuration](#configuration)&gt; | 设备的Configuration |
**示例:**
```ts
try {
this.context.resourceManager.getConfiguration().then(value => {
let direction = value.direction;
let locale = value.locale;
}).catch(error => {
console.error("getConfiguration promise error is " + error);
});
} catch (error) {
console.error("getConfiguration promise error is " + error);
}
```
### getDeviceCapability
getDeviceCapability(callback: AsyncCallback&lt;DeviceCapability&gt;): void
用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象。 | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**系统能力**:SystemCapability.Global.ResourceManager **错误码:**
**参数:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 参数名 | 类型 | 必填 | 说明 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | ---- | ---------------------------- | | -------- | ---------------------------------------- |
| callback | AsyncCallback&lt;[DeviceCapability](#devicecapability)&gt; | 是 | 异步回调,用于返回设备的DeviceCapability | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getDeviceCapability((error, value) => { this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error, value) => {
if (error != null) { if (error != null) {
console.error("getDeviceCapability callback error is " + error); console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} else { } else {
let screenDensity = value.screenDensity; let media = value;
let deviceType = value.deviceType;
} }
}); });
} catch (error) { } catch (error) {
console.error("getDeviceCapability callback error is " + error); console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>9+</sup>
### getDeviceCapability getMediaContent(resId: number): Promise&lt;Uint8Array&gt;
getDeviceCapability(): Promise&lt;DeviceCapability&gt;
用户获取设备的DeviceCapability,使用Promise形式返回DeviceCapability对象 用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ---------------------------------------- | ------------------- | | ------------------------- | -------------- |
| Promise&lt;[DeviceCapability](#devicecapability)&gt; | 设备的DeviceCapability | | Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getDeviceCapability().then(value => { this.context.resourceManager.getMediaContent($r('app.media.test').id).then((value: Uint8Array) => {
let screenDensity = value.screenDensity; let media = value;
let deviceType = value.deviceType; }).catch((error: BusinessError) => {
}).catch(error => { console.log("getMediaContent promise error is " + error);
console.error("getDeviceCapability promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error("getDeviceCapability promise error is " + error); console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringValue<sup>9+</sup> ### getMediaContent<sup>10+</sup>
getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt;
根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------- | | ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1522,44 +1539,38 @@ getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;stri ...@@ -1522,44 +1539,38 @@ getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;stri
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => { this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then((value: Uint8Array) => {
if (error != null) { let media = value;
console.log("error is " + error); }).catch((error: BusinessError) => {
} else { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
let str = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>9+</sup>
### getPluralStringValue<sup>9+</sup> getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): void
getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串 用户获取指定resource对象对应的媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 |
**返回值:** **参数:**
| 类型 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| --------------------- | ------------------------- | | -------- | ------------------------------- | ---- | ------------------ |
| Promise&lt;string&gt; | 根据提供的数量获取对应ID字符串表示的单复数字符串 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1569,26 +1580,34 @@ getPluralStringValue(resId: number, num: number): Promise&lt;string&gt; ...@@ -1569,26 +1580,34 @@ getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import resourceManager from '@ohos.resourceManager';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then(value => { this.context.resourceManager.getMediaContent(resource, (error, value) => {
let str = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.log("getPluralStringValue promise error is " + error); } else {
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringValue<sup>9+</sup> ### getMediaContent<sup>10+</sup>
getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void getMediaContent(resource: Resource, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串 用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -1596,11 +1615,11 @@ getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt ...@@ -1596,11 +1615,11 @@ getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------------ | | -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| num | number | 是 | 数量值 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串 | | callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1610,33 +1629,34 @@ getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt ...@@ -1610,33 +1629,34 @@ getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication",
moduleName: "entry", let resource: resourceManager.Resource = {
id: $r('app.plural.test').id bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
}; };
try { try {
this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => { this.context.resourceManager.getMediaContent(resource, 120, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} else { } else {
let str = value; let media = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringValue<sup>9+</sup> ### getMediaContent<sup>9+</sup>
getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt; getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt;
根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串 用户获取指定resource对象对应的媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -1647,13 +1667,12 @@ getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt; ...@@ -1647,13 +1667,12 @@ getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt;
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| num | number | 是 | 数量值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ------------------------------ | | ------------------------- | ------------------- |
| Promise&lt;string&gt; | 根据提供的数量获取对应resource对象表示的单复数字符串 | | Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1663,41 +1682,50 @@ getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt; ...@@ -1663,41 +1682,50 @@ getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication", import { BusinessError } from '@ohos.base';
moduleName: "entry",
id: $r('app.plural.test').id let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
}; };
try { try {
this.context.resourceManager.getPluralStringValue(resource, 1).then(value => { this.context.resourceManager.getMediaContent(resource).then((value: Uint8Array) => {
let str = value; let media = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getPluralStringValue promise error is " + error); console.log("getMediaContent promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>10+</sup>
### getRawFileContent<sup>9+</sup> getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt;
getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。 用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- | | -------- | ---------------------- | ---- | ---- |
| path | string | 是 | rawfile文件路径 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的rawfile文件内容 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------------- |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1705,42 +1733,44 @@ getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void ...@@ -1705,42 +1733,44 @@ getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
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 { try {
this.context.resourceManager.getRawFileContent("test.xml", (error, value) => { this.context.resourceManager.getMediaContent(resource, 120).then((value: Uint8Array) => {
if (error != null) { let media = value;
console.log("error is " + error); }).catch((error: BusinessError) => {
} else { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
let rawFile = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getRawFileContent<sup>9+</sup> ### getMediaByName<sup>9+</sup>
getRawFileContent(path: string): Promise&lt;Uint8Array&gt; getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。 用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- | | -------- | ------------------------------- | ---- | ------------------ |
| path | string | 是 | rawfile文件路径 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | rawfile文件内容 |
**错误码:** **错误码:**
...@@ -1748,36 +1778,39 @@ getRawFileContent(path: string): Promise&lt;Uint8Array&gt; ...@@ -1748,36 +1778,39 @@ getRawFileContent(path: string): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getRawFileContent("test.xml").then(value => { this.context.resourceManager.getMediaByName("test", (error, value) => {
let rawFile = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.log("getRawFileContent promise error is " + error); } else {
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaByName<sup>10+</sup>
### getRawFd<sup>9+</sup> getMediaByName(resName: string, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | -------------------------------- | | -------- | ------------------------------- | ---- | ------------------ |
| path | string | 是 | rawfile文件路径 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | 是 | 异步回调,用于返回获取的rawfile文件的descriptor | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1785,44 +1818,43 @@ getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void ...@@ -1785,44 +1818,43 @@ getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getRawFd("test.xml", (error, value) => { this.context.resourceManager.getMediaByName("test", 120, (error, value) => {
if (error != null) { if (error != null) {
console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} else { } else {
let fd = value.fd; let media = value;
let offset = value.offset; }
let length = value.length;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getRawFd failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getRawFd<sup>9+</sup> ### getMediaByName<sup>9+</sup>
getRawFd(path: string): Promise&lt;RawFileDescriptor&gt; getMediaByName(resName: string): Promise&lt;Uint8Array&gt;
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回 用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- | | ------- | ------ | ---- | ---- |
| path | string | 是 | rawfile文件路径 | | resName | string | 是 | 资源名称 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ---------------------------------------- | ------------------- | | ------------------------- | ------------- |
| Promise&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | rawfile文件descriptor | | Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1830,37 +1862,44 @@ getRawFd(path: string): Promise&lt;RawFileDescriptor&gt; ...@@ -1830,37 +1862,44 @@ getRawFd(path: string): Promise&lt;RawFileDescriptor&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getRawFd("test.xml").then(value => { this.context.resourceManager.getMediaByName("test").then((value: Uint8Array) => {
let fd = value.fd; let media = value;
let offset = value.offset; }).catch((error: BusinessError) => {
let length = value.length; console.log("getMediaByName promise error is " + error);
}).catch(error => {
console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`);
}); });
} catch (error) { } catch (error) {
console.error(`promise getRawFd failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`)
} }
``` ```
### getRawFileList<sup>10+</sup> ### getMediaByName<sup>10+</sup>
getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): void; getMediaByName(resName: string, density: number): Promise&lt;Uint8Array&gt;
用户获取resources/rawfile目录下文件夹及文件列表,使用callback形式返回文件列表的字符串数组。 用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- | | ------- | ------ | ---- | ---- |
| path | string | 是 | rawfile文件夹路径 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Array\<string\>&gt; | 是 | 异步回调,用于返回获取rawfile文件目录下的文件列表 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1868,42 +1907,38 @@ getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): vo ...@@ -1868,42 +1907,38 @@ getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): vo
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:** **示例:**
```ts ```ts
try { // 传入""表示获取rawfile根目录下的文件列表 import { BusinessError } from '@ohos.base';
this.context.resourceManager.getRawFileList("", (error, value) => {
if (error != null) { try {
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); this.context.resourceManager.getMediaByName("test", 120).then((value: Uint8Array) => {
} else { let media = value;
let rawFile = value; }).catch((error: BusinessError) => {
} console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
}); });
} catch (error) { } catch (error) {
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getRawFileList<sup>10+</sup> ### getMediaContentBase64<sup>9+</sup>
getRawFileList(path: string): Promise&lt;Array\<string\>&gt; getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void
用户获取resources/rawfile目录下文件夹及文件列表,使用Promise形式返回文件列表字符串数组 用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:** Content
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件夹路径 |
**返回值:**
| 类型 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------------------------- | ----------- | | -------- | --------------------------- | ---- | ------------------------ |
| Promise&lt;Array\<string\>&gt; | rawfile文件目录下的文件列表 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -1911,35 +1946,39 @@ getRawFileList(path: string): Promise&lt;Array\<string\>&gt; ...@@ -1911,35 +1946,39 @@ getRawFileList(path: string): Promise&lt;Array\<string\>&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
try { // 传入""表示获取rawfile根目录下的文件列表 try {
this.context.resourceManager.getRawFileList("").then(value => { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => {
let rawFile = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); } else {
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### closeRawFd<sup>9+</sup> ### getMediaContentBase64<sup>10+</sup>
closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void getMediaContentBase64(resId: number, density: number, callback: AsyncCallback&lt;string&gt;): void
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回 用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----------- | | -------- | --------------------------- | ---- | ------------------------ |
| path | string | 是 | rawfile文件路径 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;void&gt; | 是 | 异步回调 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -1947,41 +1986,43 @@ closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -1947,41 +1986,43 @@ closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001005 | The resource not found by path. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.closeRawFd("test.xml", (error, value) => { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} } else {
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### closeRawFd<sup>9+</sup> ### getMediaContentBase64<sup>9+</sup>
closeRawFd(path: string): Promise&lt;void&gt; getMediaContentBase64(resId: number): Promise&lt;string&gt;
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回 用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- | | ----- | ------ | ---- | ----- |
| path | string | 是 | rawfile文件路径 | | resId | number | 是 | 资源ID值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------- | ---- | | --------------------- | -------------------- |
| Promise&lt;void&gt; | 无返回值 | | Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -1989,52 +2030,85 @@ closeRawFd(path: string): Promise&lt;void&gt; ...@@ -1989,52 +2030,85 @@ closeRawFd(path: string): Promise&lt;void&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.closeRawFd("test.xml").then(value => { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then((value: string) => {
let result = value; let media = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("closeRawFd promise error is " + error); console.log("getMediaContentBase64 promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### release<sup>7+</sup> ### getMediaContentBase64<sup>10+</sup>
release() getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt;
用户释放创建的resourceManager 用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.release(); 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) { } catch (error) {
console.error("release error is " + error); console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringByName<sup>9+</sup> ### getMediaContentBase64<sup>9+</sup>
getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的字符串,使用callback形式返回字符串。 用户获取指定resource对象对应的图片资源Base64编码,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- | | -------- | --------------------------- | ---- | ------------------------ |
| resName | string | 是 | 资源名称 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2042,127 +2116,48 @@ getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void ...@@ -2042,127 +2116,48 @@ getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001001 | If the resId invalid. |
| 9001004 | If the resource not found by resName. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import resourceManager from '@ohos.resourceManager';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getStringByName("test", (error, value) => { this.context.resourceManager.getMediaContentBase64(resource, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let string = value; let media = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringByName<sup>9+</sup> ### getMediaContentBase64<sup>10+</sup>
getStringByName(resName: string): Promise&lt;string&gt; getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的字符串,使用Promise形式返回字符串。 用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
| 参数名 | 类型 | 必填 | 说明 | **参数:**
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:** | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| 类型 | 说明 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| --------------------- | ---------- | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| Promise&lt;string&gt; | 资源名称对应的字符串 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getStringByName("test").then(value => {
let string = value;
}).catch(error => {
console.log("getStringByName promise error is " + error);
});
} catch (error) {
console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringArrayByName<sup>9+</sup>
getStringArrayByName(resName: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
用户获取指定资源名称对应的字符串数组,使用callback形式返回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getStringArrayByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
} catch (error) {
console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringArrayByName<sup>9+</sup>
getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt;
用户获取指定资源名称对应的字符串数组,使用Promise形式返回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------ |
| Promise&lt;Array&lt;string&gt;&gt; | 资源名称对应的字符串数组 |
**错误码:** **错误码:**
...@@ -2170,121 +2165,52 @@ getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt; ...@@ -2170,121 +2165,52 @@ getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001001 | If the resId invalid. |
| 9001004 | If the resource not found by resName. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { import resourceManager from '@ohos.resourceManager';
this.context.resourceManager.getStringArrayByName("test").then(value => {
let strArray = value;
}).catch(error => {
console.log("getStringArrayByName promise error is " + error);
});
} catch (error) {
console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaByName<sup>9+</sup> let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void moduleName: "entry",
id: $r('app.media.test').id
用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。 };
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:**
```ts
try { try {
this.context.resourceManager.getMediaByName("test", (error, value) => { this.context.resourceManager.getMediaContentBase64(resource, 120, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} else { } else {
let media = value; let media = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaByName<sup>10+</sup> ### getMediaContentBase64<sup>9+</sup>
getMediaByName(resName: string, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void getMediaContentBase64(resource: Resource): Promise&lt;string&gt;
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组 用户获取指定resource对象对应的图片资源Base64编码,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:**
```ts
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) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaByName<sup>9+</sup>
getMediaByName(resName: string): Promise&lt;Uint8Array&gt;
用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
| resName | string | 是 | 资源名称 | | resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | ------------- | | --------------------- | ------------------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 | | Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2292,42 +2218,52 @@ getMediaByName(resName: string): Promise&lt;Uint8Array&gt; ...@@ -2292,42 +2218,52 @@ getMediaByName(resName: string): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001001 | If the resId invalid. |
| 9001004 | If the resource not found by resName. | | 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
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 { try {
this.context.resourceManager.getMediaByName("test").then(value => { this.context.resourceManager.getMediaContentBase64(resource).then((value: string) => {
let media = value; let media = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getMediaByName promise error is " + error); console.log("getMediaContentBase64 promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`) console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaByName<sup>10+</sup> ### getMediaContentBase64<sup>10+</sup>
getMediaByName(resName: string, density: number): Promise&lt;Uint8Array&gt; getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt;
用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组 用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
| resName | string | 是 | 资源名称 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | ------------- | | --------------------- | ------------------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 | | Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2335,19 +2271,27 @@ getMediaByName(resName: string, density: number): Promise&lt;Uint8Array&gt; ...@@ -2335,19 +2271,27 @@ getMediaByName(resName: string, density: number): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001001 | If the resId invalid. |
| 9001004 | If the resource not found by resName. | | 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
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 { try {
this.context.resourceManager.getMediaByName("test", 120).then(value => { this.context.resourceManager.getMediaContentBase64(resource, 120).then((value: string) => {
let media = value; let media = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
...@@ -2379,11 +2323,11 @@ getMediaBase64ByName(resName: string, callback: AsyncCallback&lt;string&gt;): vo ...@@ -2379,11 +2323,11 @@ getMediaBase64ByName(resName: string, callback: AsyncCallback&lt;string&gt;): vo
```ts ```ts
try { try {
this.context.resourceManager.getMediaBase64ByName("test", (error, value) => { this.context.resourceManager.getMediaBase64ByName("test", (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let media = value; let media = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
...@@ -2419,11 +2363,11 @@ getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback&l ...@@ -2419,11 +2363,11 @@ getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback&l
```ts ```ts
try { try {
this.context.resourceManager.getMediaBase64ByName("test", 120, (error, value) => { this.context.resourceManager.getMediaBase64ByName("test", 120, (error, value) => {
if (error != null) { if (error != null) {
console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
} else { } else {
let media = value; let media = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
...@@ -2461,11 +2405,13 @@ getMediaBase64ByName(resName: string): Promise&lt;string&gt; ...@@ -2461,11 +2405,13 @@ getMediaBase64ByName(resName: string): Promise&lt;string&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getMediaBase64ByName("test").then(value => { this.context.resourceManager.getMediaBase64ByName("test").then((value: string) => {
let media = value; let media = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getMediaBase64ByName promise error is " + error); console.log("getMediaBase64ByName promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
...@@ -2504,79 +2450,85 @@ getMediaBase64ByName(resName: string, density: number): Promise&lt;string&gt; ...@@ -2504,79 +2450,85 @@ getMediaBase64ByName(resName: string, density: number): Promise&lt;string&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getMediaBase64ByName("test", 120).then(value => { this.context.resourceManager.getMediaBase64ByName("test", 120).then((value: string) => {
let media = value; let media = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringByName<sup>9+</sup> ### getDrawableDescriptor<sup>10+</sup>
getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;string&gt;): void getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;
根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串 用户获取指定资源ID对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ----------------------------- | | ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称 | | resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据传入的数量值获取资源名称对应的字符串资源 |
**错误码:** **返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001001 | If the resId invalid. |
| 9001004 | If the resource not found by resName. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => { this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id);
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) { } catch (error) {
console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120);
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringByName<sup>9+</sup> ### getDrawableDescriptor<sup>10+</sup>
getPluralStringByName(resName: string, num: number): Promise&lt;string&gt; getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串 用户获取指定resource对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
| 参数名 | 类型 | 必填 | 说明 | **参数:**
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 | | 参数名 | 类型 | 必填 | 说明 |
| num | number | 是 | 数量值 | | -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ---------------------- | | ------- | ----------------- |
| Promise&lt;string&gt; | 根据传入的数量值获取资源名称对应的字符串资源 | | DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:** **错误码:**
...@@ -2584,42 +2536,50 @@ getPluralStringByName(resName: string, num: number): Promise&lt;string&gt; ...@@ -2584,42 +2536,50 @@ getPluralStringByName(resName: string, num: number): Promise&lt;string&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001001 | If the resId invalid. |
| 9001004 | If the resource not found by resName. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import resourceManager from '@ohos.resourceManager';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.icon').id
};
try { try {
this.context.resourceManager.getPluralStringByName("test", 1).then(value => { this.context.resourceManager.getDrawableDescriptor(resource);
let str = value;
}).catch(error => {
console.log("getPluralStringByName promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getDrawableDescriptor(resource, 120);
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>9+</sup> ### getDrawableDescriptorByName<sup>10+</sup>
getStringSync(resId: number): string getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor;
用户获取指定资源ID对应的字符串,使用同步方式返回字符串 用户获取指定资源名称对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ----------- | | ------ | --------- |
| string | 资源ID值对应的字符串 | | DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:** **错误码:**
...@@ -2627,24 +2587,28 @@ getStringSync(resId: number): string ...@@ -2627,24 +2587,28 @@ getStringSync(resId: number): string
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringSync($r('app.string.test').id); this.context.resourceManager.getDrawableDescriptorByName('icon');
} catch (error) { } catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getDrawableDescriptorByName('icon', 120);
} catch (error) {
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>10+</sup> ### getBoolean<sup>9+</sup>
getStringSync(resId: number, ...args: Array<string | number>): string getBoolean(resId: number): boolean
用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串 使用同步方式,返回获取指定资源ID对应的布尔结果
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -2653,39 +2617,36 @@ getStringSync(resId: number, ...args: Array<string | number>): string ...@@ -2653,39 +2617,36 @@ getStringSync(resId: number, ...args: Array<string | number>): string
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------------------------- | | ------- | ------------ |
| string | 资源ID值对应的格式化字符串| | boolean | 资源ID值对应的布尔结果 |
**错误码:** **错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ----------------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
| 9001007 | If the resource obtained by resId formatting error. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78); this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id);
} catch (error) { } catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getBoolean<sup>9+</sup>
### getStringSync<sup>9+</sup> getBoolean(resource: Resource): boolean
getStringSync(resource: Resource): string
用户获取指定resource对象对应的字符串,使用同步方式返回字符串 使用同步方式,返回获取指定resource对象对应的布尔结果
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -2699,9 +2660,9 @@ getStringSync(resource: Resource): string ...@@ -2699,9 +2660,9 @@ getStringSync(resource: Resource): string
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------------- | | ------- | ----------------- |
| string | resource对象对应的字符串 | | boolean | resource对象对应的布尔结果 |
**错误码:** **错误码:**
...@@ -2715,40 +2676,39 @@ getStringSync(resource: Resource): string ...@@ -2715,40 +2676,39 @@ getStringSync(resource: Resource): string
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication",
moduleName: "entry", let resource: resourceManager.Resource = {
id: $r('app.string.test').id bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.boolean.boolean_test').id
}; };
try { try {
this.context.resourceManager.getStringSync(resource); this.context.resourceManager.getBoolean(resource);
} catch (error) { } catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>10+</sup> ### getBooleanByName<sup>9+</sup>
getStringSync(resource: Resource, ...args: Array<string | number>): string getBooleanByName(resName: string): boolean
用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。 使用同步方式,返回获取指定资源名称对应的布尔结果
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | ------- | ------ | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resName | string | 是 | 资源名称 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------------------------- | | ------- | ----------- |
| string | resource对象对应的格式化字符串| | boolean | 资源名称对应的布尔结果 |
**错误码:** **错误码:**
...@@ -2756,44 +2716,38 @@ getStringSync(resource: Resource, ...args: Array<string | number>): string ...@@ -2756,44 +2716,38 @@ getStringSync(resource: Resource, ...args: Array<string | number>): string
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
| 9001007 | If the resource obtained by resId formatting error. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
try { try {
this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78); this.context.resourceManager.getBooleanByName("boolean_test");
} catch (error) { } catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringByNameSync<sup>9+</sup> ### getNumber<sup>9+</sup>
getStringByNameSync(resName: string): string getNumber(resId: number): number
用户获取指定资源名称对应的字符串,使用同步方式返回字符串 用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回资源对应的数值
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称 | | resId | number | 是 | 资源ID值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------- | | ------ | ---------- |
| string | 资源名称对应的字符串 | | number | 资源ID值对应的数值。Integer对应的是原数值,float对应的是真实像素点值,具体参考示例代码 |
**错误码:** **错误码:**
...@@ -2801,39 +2755,46 @@ getStringByNameSync(resName: string): string ...@@ -2801,39 +2755,46 @@ getStringByNameSync(resName: string): string
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001001 | If the resId invalid. |
| 9001004 | If the resource not found by resName. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringByNameSync("test"); this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer对应返回的是原数值
} catch (error) { } catch (error) {
console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getNumber($r('app.float.float_test').id); // float对应返回的是真实像素点值
} catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringByNameSync<sup>10+</sup> ### getNumber<sup>9+</sup>
getStringByNameSync(resName: string, ...args: Array<string | number>): string getNumber(resource: Resource): number
用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串 用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回资源对应的数值
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
| resName | string | 是 | 资源名称 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------------------------- | | ------ | --------------- |
| string | 资源名称对应的格式化字符串| | number | resource对象对应的数值。Integer对应的是原数值,float对应的是真实像素点值, 具体参考示例代码 |
**错误码:** **错误码:**
...@@ -2841,40 +2802,91 @@ getStringByNameSync(resName: string, ...args: Array<string | number>): string ...@@ -2841,40 +2802,91 @@ getStringByNameSync(resName: string, ...args: Array<string | number>): string
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001001 | If the resId invalid. |
| 9001004 | If the resource not found by resName. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
| 9001008 | If the resource obtained by resName formatting error. |
**示例:** **示例:**
```ts ```ts
import resourceManager from '@ohos.resourceManager';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.integer.integer_test').id
};
try { try {
this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78); this.context.resourceManager.getNumber(resource);// integer对应返回的是原数值, float对应返回的是真实像素点值
} catch (error) { } catch (error) {
console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getBoolean<sup>9+</sup> ### getNumberByName<sup>9+</sup>
getBoolean(resId: number): boolean getNumberByName(resName: string): number
使用同步方式,返回获取指定资源ID对应的布尔结果 用户获取指定资源名称对应的integer数值或者float数值,使用同步方式资源对应的数值
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------- | ------------ | | ------ | --------- |
| boolean | 资源ID值对应的布尔结果 | | number | 资源名称对应的数值 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getNumberByName("integer_test");
} catch (error) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getNumberByName("float_test");
} catch (error) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColorSync<sup>10+</sup>
getColorSync(resId: number) : number;
用户获取指定资源ID对应的颜色值,使用同步方式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| number | 资源ID值对应的颜色值(十进制) |
**错误码:** **错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
...@@ -2885,25 +2897,26 @@ getBoolean(resId: number): boolean ...@@ -2885,25 +2897,26 @@ getBoolean(resId: number): boolean
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id); this.context.resourceManager.getColorSync($r('app.color.test').id);
} catch (error) { } catch (error) {
console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getBoolean<sup>9+</sup>
getBoolean(resource: Resource): boolean ### getColorSync<sup>10+</sup>
使用同步方式,返回获取指定resource对象对应的布尔结果。 getColorSync(resource: Resource): number
用户获取指定resource对象对应的颜色值,使用同步方式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
...@@ -2911,9 +2924,9 @@ getBoolean(resource: Resource): boolean ...@@ -2911,9 +2924,9 @@ getBoolean(resource: Resource): boolean
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------- | ----------------- | | ------ | ---------------- |
| boolean | resource对象对应的布尔结果 | | number | resource对象对应的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -2925,29 +2938,31 @@ getBoolean(resource: Resource): boolean ...@@ -2925,29 +2938,31 @@ getBoolean(resource: Resource): boolean
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication",
moduleName: "entry", let resource: resourceManager.Resource = {
id: $r('app.boolean.boolean_test').id bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
}; };
try { try {
this.context.resourceManager.getBoolean(resource); this.context.resourceManager.getColorSync(resource);
} catch (error) { } catch (error) {
console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getBooleanByName<sup>9+</sup> ### getColorByNameSync<sup>10+</sup>
getBooleanByName(resName: string): boolean getColorByNameSync(resName: string) : number;
使用同步方式,返回获取指定资源名称对应的布尔结果 用户获取指定资源名称对应的颜色值,使用同步方式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ------- | ------ | ---- | ---- |
...@@ -2955,9 +2970,9 @@ getBooleanByName(resName: string): boolean ...@@ -2955,9 +2970,9 @@ getBooleanByName(resName: string): boolean
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------- | ----------- | | ------ | ---------- |
| boolean | 资源名称对应的布尔结果 | | number | 资源名称对应的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -2969,24 +2984,64 @@ getBooleanByName(resName: string): boolean ...@@ -2969,24 +2984,64 @@ getBooleanByName(resName: string): boolean
| 9001004 | If the resource not found by resName. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getBooleanByName("boolean_test"); this.context.resourceManager.getColorByNameSync("test");
} catch (error) { } catch (error) {
console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getColorByNameSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getNumber<sup>9+</sup> ### getColor<sup>10+</sup>
getNumber(resId: number): number getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void;
用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回资源对应的数值。 用户获取指定资源ID对应的颜色值,使用callback形式返回其对应的颜色值。
**系统能力:** SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the module resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例Stage:**
```ts
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) {
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColor<sup>10+</sup>
getColor(resId: number): Promise&lt;number&gt;
用户获取指定资源ID对应的颜色值,使用Promise形式返回对应其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ----- | ------ | ---- | ----- |
...@@ -2994,9 +3049,9 @@ getNumber(resId: number): number ...@@ -2994,9 +3049,9 @@ getNumber(resId: number): number
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------- | | --------------------- | ----------- |
| number | 资源ID值对应的数值。Integer对应的是原数值,float对应的是真实像素点值,具体参考示例代码 | | Promise&lt;number&gt; | 资源ID值对应的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -3008,32 +3063,81 @@ getNumber(resId: number): number ...@@ -3008,32 +3063,81 @@ getNumber(resId: number): number
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer对应返回的是原数值 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) { } catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`);
} }
```
### getColor<sup>10+</sup>
getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void;
用户获取指定resource对象对应的颜色值,使用callback形式返回其对应的颜色值。
**系统能力:** SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
import resourceManager from '@ohos.resourceManager';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try { try {
this.context.resourceManager.getNumber($r('app.float.float_test').id); // float对应返回的是真实像素点值 this.context.resourceManager.getColor(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) { } catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getNumber<sup>9+</sup> ### getColor<sup>10+</sup>
getNumber(resource: Resource): number getColor(resource: Resource): Promise&lt;number&gt;;
用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回资源对应的数值。 用户获取指定resource对象对应的颜色值,使用Promise形式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
...@@ -3041,9 +3145,9 @@ getNumber(resource: Resource): number ...@@ -3041,9 +3145,9 @@ getNumber(resource: Resource): number
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | --------------- | | --------------------- | ---------------- |
| number | resource对象对应的数值。Integer对应的是原数值,float对应的是真实像素点值, 具体参考示例代码 | | Promise&lt;number&gt; | resource对象对应的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -3055,39 +3159,41 @@ getNumber(resource: Resource): number ...@@ -3055,39 +3159,41 @@ getNumber(resource: Resource): number
| 9001002 | If the resource not found by resId. | | 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import resourceManager from '@ohos.resourceManager';
bundleName: "com.example.myapplication", import { BusinessError } from '@ohos.base';
moduleName: "entry",
id: $r('app.integer.integer_test').id let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
}; };
try { try {
this.context.resourceManager.getNumber(resource);// integer对应返回的是原数值, float对应返回的是真实像素点值 this.context.resourceManager.getColor(resource).then((value: number) => {
let str = value;
}).catch((error: BusinessError) => {
console.log("getColor promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getNumberByName<sup>9+</sup> ### getColorByName<sup>10+</sup>
getNumberByName(resName: string): number getColorByName(resName: string, callback: AsyncCallback&lt;number&gt;): void
用户获取指定资源名称对应的integer数值或者float数值,使用同步方式资源对应的数值。 用户获取指定资源名称对应的颜色值,使用callback形式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------ | --------- | | -------- | --------------------------- | ---- | --------------- |
| number | 资源名称对应的数值 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -3099,41 +3205,40 @@ getNumberByName(resName: string): number ...@@ -3099,41 +3205,40 @@ getNumberByName(resName: string): number
| 9001004 | If the resource not found by resName. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. | | 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getNumberByName("integer_test"); this.context.resourceManager.getColorByName("test", (error, value) => {
} catch (error) { if (error != null) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`); console.log("error is " + error);
} } else {
let string = value;
try { }
this.context.resourceManager.getNumberByName("float_test"); });
} catch (error) { } catch (error) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getColorByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getDrawableDescriptor<sup>10+</sup> ### getColorByName<sup>10+</sup>
getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor; getColorByName(resName: string): Promise&lt;number&gt;
用户获取指定资源ID对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示 用户获取指定资源名称对应的颜色值,使用Promise形式返回其对应的颜色值
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------- | | --------------------- | ---------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | | Promise&lt;number&gt; | 资源名称对应的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -3141,45 +3246,39 @@ getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor; ...@@ -3141,45 +3246,39 @@ getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001003 | If the resName invalid. |
| 9001002 | If the resource not found by resId. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id); this.context.resourceManager.getColorByName("test").then((value: number) => {
} catch (error) { let string = value;
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`); }).catch((error: BusinessError) => {
} console.log("getColorByName promise error is " + error);
try { });
this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120);
} catch (error) { } catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getColorByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getDrawableDescriptor<sup>10+</sup> ### getRawFileContent<sup>9+</sup>
getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor; getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取指定resource对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示 用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **参数:**
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:**
| 类型 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ----------------- | | -------- | ------------------------------- | ---- | ----------------------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | | path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的rawfile文件内容 |
**错误码:** **错误码:**
...@@ -3187,48 +3286,42 @@ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor; ...@@ -3187,48 +3286,42 @@ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001005 | If the resource not found by path. |
| 9001002 | If the resource not found by resId. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.icon').id
};
try {
this.context.resourceManager.getDrawableDescriptor(resource);
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
}
try { try {
this.context.resourceManager.getDrawableDescriptor(resource, 120); this.context.resourceManager.getRawFileContent("test.txt", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let rawFile = value;
}
});
} catch (error) { } catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getDrawableDescriptorByName<sup>10+</sup> ### getRawFileContent<sup>9+</sup>
getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor; getRawFileContent(path: string): Promise&lt;Uint8Array&gt;
用户获取指定资源名称对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示 用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ---- | ------ | ---- | ----------- |
| resName | string | 是 | 资源名称 | | path | string | 是 | rawfile文件路径 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | --------- | | ------------------------- | ----------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | | Promise&lt;Uint8Array&gt; | rawfile文件内容 |
**错误码:** **错误码:**
...@@ -3236,37 +3329,37 @@ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescript ...@@ -3236,37 +3329,37 @@ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescript
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001005 | If the resource not found by path. |
| 9001004 | If the resource not found by resName. |
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
try { try {
this.context.resourceManager.getDrawableDescriptorByName('icon'); this.context.resourceManager.getRawFileContent("test.txt").then((value: Uint8Array) => {
} catch (error) { let rawFile = value;
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`); }).catch((error: BusinessError) => {
} console.log("getRawFileContent promise error is " + error);
try { });
this.context.resourceManager.getDrawableDescriptorByName('icon', 120);
} catch (error) { } catch (error) {
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getColor<sup>10+</sup> ### getRawFileList<sup>10+</sup>
getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void; getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): void;
用户获取指定资源ID对应的颜色值,使用callback形式返回其对应的颜色值 用户获取resources/rawfile目录下文件夹及文件列表,使用callback形式返回文件列表的字符串数组
**系统能力:** SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- | | -------- | ------------------------------- | ---- | ----------------------- |
| resId | number | 是 | 资源ID值 | | path | string | 是 | rawfile文件夹路径 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) | | callback | AsyncCallback&lt;Array\<string\>&gt; | 是 | 异步回调,用于返回获取rawfile文件目录下的文件列表 |
**错误码:** **错误码:**
...@@ -3274,44 +3367,42 @@ getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void; ...@@ -3274,44 +3367,42 @@ getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the module resId invalid. | | 9001005 | If the resource not found by path. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例Stage:** **示例:**
```ts ```ts
try { try { // 传入""表示获取rawfile根目录下的文件列表
this.context.resourceManager.getColor($r('app.color.test').id, (error, value) => { this.context.resourceManager.getRawFileList("", (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
} else { } else {
let str = value; let rawFile = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getColor<sup>10+</sup> ### getRawFileList<sup>10+</sup>
getColor(resId: number): Promise&lt;number&gt; getRawFileList(path: string): Promise&lt;Array\<string\>&gt;
用户获取指定资源ID对应的颜色值,使用Promise形式返回对应其对应的颜色值 用户获取resources/rawfile目录下文件夹及文件列表,使用Promise形式返回文件列表字符串数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ---- | ------ | ---- | ----------- |
| resId | number | 是 | 资源ID值 | | path | string | 是 | rawfile文件夹路径 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ----------- | | ------------------------- | ----------- |
| Promise&lt;number&gt; | 资源ID值对应的颜色值(十进制) | | Promise&lt;Array\<string\>&gt; | rawfile文件目录下的文件列表 |
**错误码:** **错误码:**
...@@ -3319,39 +3410,37 @@ getColor(resId: number): Promise&lt;number&gt; ...@@ -3319,39 +3410,37 @@ getColor(resId: number): Promise&lt;number&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001005 | If the resource not found by path. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { import { BusinessError } from '@ohos.base';
this.context.resourceManager.getColor($r('app.color.test').id).then(value => {
let str = value; try { // 传入""表示获取rawfile根目录下的文件列表
}).catch(error => { this.context.resourceManager.getRawFileList("").then((value: Array<string>) => {
console.log("getColor promise error is " + error); let rawFile = value;
}).catch((error: BusinessError) => {
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
}); });
} catch (error) { } catch (error) {
console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getColor<sup>10+</sup> ### getRawFd<sup>9+</sup>
getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void;
用户获取指定resource对象对应的颜色值,使用callback形式返回其对应的颜色值。 getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
**系统能力:** SystemCapability.Global.ResourceManager 用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回。
**模型约束**:此接口仅可在Stage模型下使用。 **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- | | -------- | ---------------------------------------- | ---- | -------------------------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) | | callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | 是 | 异步回调,用于返回获取的rawfile文件的descriptor |
**错误码:** **错误码:**
...@@ -3359,51 +3448,44 @@ getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void; ...@@ -3359,51 +3448,44 @@ getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001005 | If the resource not found by path. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try { try {
this.context.resourceManager.getColor(resource, (error, value) => { this.context.resourceManager.getRawFd("test.txt", (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`);
} else { } else {
let str = value; let fd = value.fd;
} let offset = value.offset;
let length = value.length;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getRawFd failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getColor<sup>10+</sup> ### getRawFd<sup>9+</sup>
getColor(resource: Resource): Promise&lt;number&gt;; getRawFd(path: string): Promise&lt;RawFileDescriptor&gt;
用户获取指定resource对象对应的颜色值,使用Promise形式返回其对应的颜色值 用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **参数:**
**参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | ---- | ------ | ---- | ----------- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | path | string | 是 | rawfile文件路径 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ---------------- | | ---------------------------------------- | ------------------- |
| Promise&lt;number&gt; | resource对象对应的颜色值(十进制) | | Promise&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | rawfile文件descriptor |
**错误码:** **错误码:**
...@@ -3411,42 +3493,39 @@ getColor(resource: Resource): Promise&lt;number&gt;; ...@@ -3411,42 +3493,39 @@ getColor(resource: Resource): Promise&lt;number&gt;;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. | | 9001005 | If the resource not found by path. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
let resource = { import { BusinessError } from '@ohos.base';
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try { try {
this.context.resourceManager.getColor(resource).then(value => { this.context.resourceManager.getRawFd("test.txt").then((value: resourceManager.RawFileDescriptor) => {
let str = value; let fd = value.fd;
}).catch(error => { let offset = value.offset;
console.log("getColor promise error is " + error); let length = value.length;
}).catch((error: BusinessError) => {
console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`);
}); });
} catch (error) { } catch (error) {
console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getRawFd failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getColorByName<sup>10+</sup> ### closeRawFd<sup>9+</sup>
getColorByName(resName: string, callback: AsyncCallback&lt;number&gt;): void closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void
用户获取指定资源名称对应的颜色值,使用callback形式返回其对应的颜色值 用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- | | -------- | ------------------------- | ---- | ----------- |
| resName | string | 是 | 资源名称 | | path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) | | callback | AsyncCallback&lt;void&gt; | 是 | 异步回调 |
**错误码:** **错误码:**
...@@ -3454,44 +3533,40 @@ getColorByName(resName: string, callback: AsyncCallback&lt;number&gt;): void ...@@ -3454,44 +3533,40 @@ getColorByName(resName: string, callback: AsyncCallback&lt;number&gt;): void
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001005 | The resource not found by path. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getColorByName("test", (error, value) => { this.context.resourceManager.closeRawFd("test.txt", (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { }
let string = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getColorByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getColorByName<sup>10+</sup> ### closeRawFd<sup>9+</sup>
getColorByName(resName: string): Promise&lt;number&gt; closeRawFd(path: string): Promise&lt;void&gt;
用户获取指定资源名称对应的颜色值,使用Promise形式返回其对应的颜色值 用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ---- | ------ | ---- | ----------- |
| resName | string | 是 | 资源名称 | | path | string | 是 | rawfile文件路径 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ---------- | | ------------------- | ---- |
| Promise&lt;number&gt; | 资源名称对应的颜色值(十进制) | | Promise&lt;void&gt; | 无返回值 |
**错误码:** **错误码:**
...@@ -3499,144 +3574,151 @@ getColorByName(resName: string): Promise&lt;number&gt; ...@@ -3499,144 +3574,151 @@ getColorByName(resName: string): Promise&lt;number&gt;
| 错误码ID | 错误信息 | | 错误码ID | 错误信息 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001005 | If the resource not found by path. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getColorByName("test").then(value => { this.context.resourceManager.closeRawFd("test.txt");
let string = value;
}).catch(error => {
console.log("getColorByName promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`promise getColorByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getColorSync<sup>10+</sup> ### getConfiguration
getColorSync(resId: number) : number; getConfiguration(callback: AsyncCallback&lt;Configuration&gt;): void
用户获取指定资源ID对应的颜色值,使用同步方式返回其对应的颜色值 用户获取设备的Configuration,使用callback形式返回Configuration对象
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| number | 资源ID值对应的颜色值(十进制) |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- | ---- | ------------------------- |
| 9001001 | If the resId invalid. | | callback | AsyncCallback&lt;[Configuration](#configuration)&gt; | 是 | 异步回调,用于返回设备的Configuration |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getColorSync($r('app.color.test').id); 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) { } catch (error) {
console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`); console.error("getConfiguration callback error is " + error);
} }
``` ```
### getColorSync<sup>10+</sup> ### getConfiguration
getColorSync(resource: Resource): number getConfiguration(): Promise&lt;Configuration&gt;
用户获取指定resource对象对应的颜色值,使用同步方式返回其对应的颜色值 用户获取设备的Configuration,使用Promise形式返回Configuration对象
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **返回值:**
**参数:** | 类型 | 说明 |
| ---------------------------------------- | ---------------- |
| Promise&lt;[Configuration](#configuration)&gt; | 设备的Configuration |
| 参数名 | 类型 | 必填 | 说明 | **示例:**
| -------- | ---------------------- | ---- | ---- | ```ts
| resource | [Resource](#resource9) | 是 | 资源信息 | 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);
}
```
| 类型 | 说明 | ### getDeviceCapability
| ------ | ---------------- |
| number | resource对象对应的颜色值(十进制) |
**错误码:** getDeviceCapability(callback: AsyncCallback&lt;DeviceCapability&gt;): void
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md) 用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象
| 错误码ID | 错误信息 | **系统能力**:SystemCapability.Global.ResourceManager
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ---------------------------- |
| callback | AsyncCallback&lt;[DeviceCapability](#devicecapability)&gt; | 是 | 异步回调,用于返回设备的DeviceCapability |
**示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try { try {
this.context.resourceManager.getColorSync(resource); 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) { } catch (error) {
console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`); console.error("getDeviceCapability callback error is " + error);
} }
``` ```
### getColorByNameSync<sup>10+</sup> ### getDeviceCapability
getColorByNameSync(resName: string) : number; getDeviceCapability(): Promise&lt;DeviceCapability&gt;
用户获取指定资源名称对应的颜色值,使用同步方式返回其对应的颜色值 用户获取设备的DeviceCapability,使用Promise形式返回DeviceCapability对象
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **返回值:**
| 参数名 | 类型 | 必填 | 说明 | | 类型 | 说明 |
| ------- | ------ | ---- | ---- | | ---------------------------------------- | ------------------- |
| resName | string | 是 | 资源名称 | | Promise&lt;[DeviceCapability](#devicecapability)&gt; | 设备的DeviceCapability |
**返回值:** **示例:**
```ts
import { BusinessError } from '@ohos.base';
| 类型 | 说明 | try {
| ------ | ---------- | this.context.resourceManager.getDeviceCapability().then((value: resourceManager.DeviceCapability) => {
| number | 资源名称对应的颜色值(十进制) | 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);
}
```
**错误码:** ### release<sup>7+</sup>
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 release()
| 错误码ID | 错误信息 | 用户释放创建的resourceManager。
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **系统能力**:SystemCapability.Global.ResourceManager
**示例:**
```ts ```ts
try { try {
this.context.resourceManager.getColorByNameSync("test"); this.context.resourceManager.release();
} catch (error) { } catch (error) {
console.error(`getColorByNameSync failed, error code: ${error.code}, message: ${error.message}.`); console.error("release error is " + error);
} }
``` ```
...@@ -3666,9 +3748,9 @@ addResource(path: string) : void; ...@@ -3666,9 +3748,9 @@ addResource(path: string) : void;
```ts ```ts
let path = getContext().bundleCodeDir + "/library1-default-signed.hsp"; let path = getContext().bundleCodeDir + "/library1-default-signed.hsp";
try { try {
this.context.resourceManager.addResource(path); this.context.resourceManager.addResource(path);
} catch (error) { } catch (error) {
console.error(`addResource failed, error code: ${error.code}, message: ${error.message}.`); console.error(`addResource failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
...@@ -3698,9 +3780,9 @@ removeResource(path: string) : void; ...@@ -3698,9 +3780,9 @@ removeResource(path: string) : void;
```ts ```ts
let path = getContext().bundleCodeDir + "/library1-default-signed.hsp"; let path = getContext().bundleCodeDir + "/library1-default-signed.hsp";
try { try {
this.resmgr.removeResource(path); this.context.resourceManager.removeResource(path);
} catch (error) { } catch (error) {
console.error(`removeResource failed, error code: ${error.code}, message: ${error.message}.`); console.error(`removeResource failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
...@@ -3721,7 +3803,7 @@ getString(resId: number, callback: AsyncCallback&lt;string&gt;): void ...@@ -3721,7 +3803,7 @@ getString(resId: number, callback: AsyncCallback&lt;string&gt;): void
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**示例:** **示例:**
```ts ```ts
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getString($r('app.string.test').id, (error, value) => { mgr.getString($r('app.string.test').id, (error, value) => {
...@@ -3759,10 +3841,12 @@ getString(resId: number): Promise&lt;string&gt; ...@@ -3759,10 +3841,12 @@ getString(resId: number): Promise&lt;string&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getString($r('app.string.test').id).then(value => { mgr.getString($r('app.string.test').id).then((value: string) => {
let str = value; let str = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getstring promise error is " + error); console.log("getstring promise error is " + error);
}); });
}); });
...@@ -3824,10 +3908,12 @@ getStringArray(resId: number): Promise&lt;Array&lt;string&gt;&gt; ...@@ -3824,10 +3908,12 @@ getStringArray(resId: number): Promise&lt;Array&lt;string&gt;&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getStringArray($r('app.strarray.test').id).then(value => { mgr.getStringArray($r('app.strarray.test').id).then((value: Array<string>) => {
let strArray = value; let strArray = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getStringArray promise error is " + error); console.log("getStringArray promise error is " + error);
}); });
}); });
...@@ -3889,10 +3975,12 @@ getMedia(resId: number): Promise&lt;Uint8Array&gt; ...@@ -3889,10 +3975,12 @@ getMedia(resId: number): Promise&lt;Uint8Array&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getMedia($r('app.media.test').id).then(value => { mgr.getMedia($r('app.media.test').id).then((value: Uint8Array) => {
let media = value; let media = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getMedia promise error is " + error); console.log("getMedia promise error is " + error);
}); });
}); });
...@@ -3954,10 +4042,12 @@ getMediaBase64(resId: number): Promise&lt;string&gt; ...@@ -3954,10 +4042,12 @@ getMediaBase64(resId: number): Promise&lt;string&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getMediaBase64($r('app.media.test').id).then(value => { mgr.getMediaBase64($r('app.media.test').id).then((value: string) => {
let media = value; let media = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getMediaBase64 promise error is " + error); console.log("getMediaBase64 promise error is " + error);
}); });
}); });
...@@ -3989,10 +4079,12 @@ getPluralString(resId: number, num: number): Promise&lt;string&gt; ...@@ -3989,10 +4079,12 @@ getPluralString(resId: number, num: number): Promise&lt;string&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getPluralString($r("app.plural.test").id, 1).then(value => { mgr.getPluralString($r("app.plural.test").id, 1).then((value: string) => {
let str = value; let str = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getPluralString promise error is " + error); console.log("getPluralString promise error is " + error);
}); });
}); });
...@@ -4051,7 +4143,7 @@ getRawFile(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void ...@@ -4051,7 +4143,7 @@ getRawFile(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
**示例:** **示例:**
```ts ```ts
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFile("test.xml", (error, value) => { mgr.getRawFile("test.txt", (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
...@@ -4086,10 +4178,12 @@ getRawFile(path: string): Promise&lt;Uint8Array&gt; ...@@ -4086,10 +4178,12 @@ getRawFile(path: string): Promise&lt;Uint8Array&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFile("test.xml").then(value => { mgr.getRawFile("test.txt").then((value: Uint8Array) => {
let rawFile = value; let rawFile = value;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getRawFile promise error is " + error); console.log("getRawFile promise error is " + error);
}); });
}); });
...@@ -4116,7 +4210,7 @@ getRawFileDescriptor(path: string, callback: AsyncCallback&lt;RawFileDescriptor& ...@@ -4116,7 +4210,7 @@ getRawFileDescriptor(path: string, callback: AsyncCallback&lt;RawFileDescriptor&
**示例:** **示例:**
```ts ```ts
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFileDescriptor("test.xml", (error, value) => { mgr.getRawFileDescriptor("test.txt", (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
...@@ -4152,12 +4246,14 @@ getRawFileDescriptor(path: string): Promise&lt;RawFileDescriptor&gt; ...@@ -4152,12 +4246,14 @@ getRawFileDescriptor(path: string): Promise&lt;RawFileDescriptor&gt;
**示例:** **示例:**
```ts ```ts
import { BusinessError } from '@ohos.base';
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFileDescriptor("test.xml").then(value => { mgr.getRawFileDescriptor("test.txt").then((value: resourceManager.RawFileDescriptor) => {
let fd = value.fd; let fd = value.fd;
let offset = value.offset; let offset = value.offset;
let length = value.length; let length = value.length;
}).catch(error => { }).catch((error: BusinessError) => {
console.log("getRawFileDescriptor promise error is " + error); console.log("getRawFileDescriptor promise error is " + error);
}); });
}); });
...@@ -4183,7 +4279,7 @@ closeRawFileDescriptor(path: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -4183,7 +4279,7 @@ closeRawFileDescriptor(path: string, callback: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```ts ```ts
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.closeRawFileDescriptor("test.xml", (error, value) => { mgr.closeRawFileDescriptor("test.txt", (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} }
...@@ -4216,10 +4312,6 @@ closeRawFileDescriptor(path: string): Promise&lt;void&gt; ...@@ -4216,10 +4312,6 @@ closeRawFileDescriptor(path: string): Promise&lt;void&gt;
**示例:** **示例:**
```ts ```ts
resourceManager.getResourceManager((error, mgr) => { resourceManager.getResourceManager((error, mgr) => {
mgr.closeRawFileDescriptor("test.xml").then(value => { mgr.closeRawFileDescriptor("test.txt");
let result = value;
}).catch(error => {
console.log("closeRawFileDescriptor promise error is " + error);
});
}); });
``` ```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册