未验证 提交 408b7db1 编写于 作者: O openharmony_ci 提交者: Gitee

!22456 资源管理新增同步接口描述,规划接口排版

Merge pull request !22456 from zt147369/master
...@@ -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<ResourceManager>): void ...@@ -48,17 +48,17 @@ getResourceManager(callback: AsyncCallback<ResourceManager>): 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中找到。
...@@ -107,15 +107,15 @@ getResourceManager(): Promise<ResourceManager> ...@@ -107,15 +107,15 @@ getResourceManager(): Promise<ResourceManager>
**示例:** **示例:**
```js ```js
resourceManager.getResourceManager().then(mgr => { resourceManager.getResourceManager().then(mgr => {
mgr.getStringValue(0x1000000, (error, value) => { mgr.getStringValue(0x1000000, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let str = value; let str = value;
} }
}); });
}).catch(error => { }).catch(error => {
console.log("error is " + error); console.log("error is " + error);
}); });
``` ```
注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。 注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。
...@@ -150,7 +150,6 @@ getResourceManager(bundleName: string): Promise<ResourceManager> ...@@ -150,7 +150,6 @@ getResourceManager(bundleName: string): Promise<ResourceManager>
}); });
``` ```
## resourceManager.getSystemResourceManager<sup>10+</sup> ## resourceManager.getSystemResourceManager<sup>10+</sup>
getSystemResourceManager(): ResourceManager getSystemResourceManager(): ResourceManager
...@@ -177,16 +176,16 @@ getSystemResourceManager(): ResourceManager ...@@ -177,16 +176,16 @@ getSystemResourceManager(): ResourceManager
```js ```js
import resourceManager from '@ohos.resourceManager'; import resourceManager from '@ohos.resourceManager';
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 => {
let str = value; let str = value;
}).catch(error => { }).catch(error => {
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 +300,25 @@ try { ...@@ -301,20 +300,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 +326,24 @@ getStringValue(resId: number, callback: AsyncCallback&lt;string&gt;): void ...@@ -322,31 +326,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 +352,39 @@ getStringValue(resId: number): Promise&lt;string&gt; ...@@ -355,42 +352,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 +392,15 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void ...@@ -398,10 +392,15 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- | | -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------------- |
| string | resource对象对应的字符串 |
**错误码:** **错误码:**
...@@ -416,46 +415,39 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void ...@@ -416,46 +415,39 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
**示例:** **示例:**
```ts ```ts
let resource = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.string.test').id 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 +458,41 @@ getStringValue(resource: Resource): Promise&lt;string&gt; ...@@ -466,40 +458,41 @@ 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 = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.string.test').id 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 +500,39 @@ getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt ...@@ -507,45 +500,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 +540,34 @@ getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt; ...@@ -553,39 +540,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 +575,44 @@ getStringArrayValue(resource: Resource, callback: AsyncCallback&lt;Array&lt;stri ...@@ -593,51 +575,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 +626,33 @@ getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt; ...@@ -651,36 +626,33 @@ getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt;
**示例:** **示例:**
```ts ```ts
let resource = {
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 => {
let strArray = value; let str = value;
}).catch(error => { }).catch(error => {
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 +662,49 @@ getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void ...@@ -690,37 +662,49 @@ 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
let 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 +714,85 @@ getMediaContent(resId: number, density: number, callback: AsyncCallback&lt;Uint8 ...@@ -730,41 +714,85 @@ 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
let 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 => {
if (error != null) { let str = value;
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); }).catch(error => {
} 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值 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;string&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.getStringByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringByName<sup>9+</sup>
getStringByName(resName: string): Promise&lt;string&gt;
用户获取指定资源名称对应的字符串,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | -------------- | | --------------------- | ---------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 | | Promise&lt;string&gt; | 资源名称对应的字符串 |
**错误码:** **错误码:**
...@@ -772,27 +800,28 @@ getMediaContent(resId: number): Promise&lt;Uint8Array&gt; ...@@ -772,27 +800,28 @@ 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").then(value => {
let media = value; let str = value;
}).catch(error => { }).catch(error => {
console.log("getMediaContent promise error is " + error); 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>10+</sup> ### getStringArrayValueSync<sup>10+</sup>
getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt; getStringArrayValueSync(resId: number): Array&lt;string&gt;
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。 用户获取指定资源ID对应的字符串数组,使用同步方式返回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -801,13 +830,12 @@ getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt; ...@@ -801,13 +830,12 @@ getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt;
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | -------------- | | --------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 | | Array&lt;string&gt; | 资源ID值对应的字符串数组 |
**错误码:** **错误码:**
...@@ -817,25 +845,22 @@ getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt; ...@@ -817,25 +845,22 @@ getMediaContent(resId: number, 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
try { try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then(value => { this.context.resourceManager.getStringArrayValueSync($r('app.strarray.test').id);
let media = value;
}).catch(error => {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) { } catch (error) {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getStringArrayValueSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>9+</sup> ### getStringArrayValueSync<sup>10+</sup>
getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): void getStringArrayValueSync(resource: Resource): Array&lt;string&gt;
用户获取指定resource对象对应的媒体文件内容,使用callback形式返回字节数组。 用户获取指定resource对象对应的字符串数组,使用同步方式返回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -843,10 +868,15 @@ getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): ...@@ -843,10 +868,15 @@ getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;):
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ | | ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Array&lt;string&gt; | resource对象对应的字符串数组 |
**错误码:** **错误码:**
...@@ -856,44 +886,36 @@ getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): ...@@ -856,44 +886,36 @@ 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 = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.media.test').id id: $r('app.strarray.test').id
}; };
try { try {
this.context.resourceManager.getMediaContent(resource, (error, value) => { this.context.resourceManager.getStringArrayValueSync(resource);
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) { } catch (error) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getStringArrayValueSync 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, 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值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -903,48 +925,42 @@ getMediaContent(resource: Resource, density: number, callback: AsyncCallback&lt; ...@@ -903,48 +925,42 @@ 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 = {
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, (error, value) => {
if (error != null) { if (error != null) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 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>9+</sup> ### getStringArrayValue<sup>9+</sup>
getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt; getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt;
用户获取指定resource对象对应的媒体文件内容,使用Promise形式返回字节数组。 用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resId | number | 是 | 资源ID值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | ------------------- | | ---------------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 | | Promise&lt;Array&lt;string&gt;&gt; | 资源ID值对应的字符串数组 |
**错误码:** **错误码:**
...@@ -954,30 +970,26 @@ getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt; ...@@ -954,30 +970,26 @@ 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 = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaContent(resource).then(value => { this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then(value => {
let media = value; let strArray = value;
}).catch(error => { }).catch(error => {
console.log("getMediaContent promise error is " + error); console.log("getStringArrayValue 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}.`);
} }
``` ```
### getMediaContent<sup>10+</sup> ### getStringArrayValue<sup>9+</sup>
getMediaContent(resource: Resource, density: number): 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
...@@ -985,16 +997,10 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt; ...@@ -985,16 +997,10 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt;
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ---------------------------------------- | ---- | ----------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------------- |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1004,39 +1010,49 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt; ...@@ -1004,39 +1010,49 @@ 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 = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.media.test').id id: $r('app.strarray.test').id
}; };
try { try {
this.context.resourceManager.getMediaContent(resource, 120).then(value => { this.context.resourceManager.getStringArrayValue(resource, (error, value) => {
let media = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } 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}.`);
} }
``` ```
### getMediaContentBase64<sup>9+</sup> ### getStringArrayValue<sup>9+</sup>
getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt;
用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串 用户获取指定resource对象对应的字符串数组,使用Promise形式返回字符串数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | -------- | ---------------------- | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------------ |
| Promise&lt;Array&lt;string&gt;&gt; | resource对象对应的字符串数组 |
**错误码:** **错误码:**
...@@ -1046,37 +1062,40 @@ getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): voi ...@@ -1046,37 +1062,40 @@ getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): voi
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 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.strarray.test').id
};
try { try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => { this.context.resourceManager.getStringArrayValue(resource).then(value => {
if (error != null) { let strArray = value;
console.log("error is " + error); }).catch(error => {
} else { console.log("getStringArray 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 getStringArrayValue 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, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串 用户获取指定资源名称对应的字符串数组,使用callback形式返回字符串数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | -------- | ---------------------------------------- | ---- | ----------------- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -1084,43 +1103,44 @@ getMediaContentBase64(resId: number, density: number, callback: AsyncCallback&lt ...@@ -1084,43 +1103,44 @@ 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
try { try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => { this.context.resourceManager.getStringArrayByName("test", (error, value) => {
if (error != null) { if (error != null) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 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>9+</sup> ### getStringArrayByName<sup>9+</sup>
getMediaContentBase64(resId: number): Promise&lt;string&gt; getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt;
用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串 用户获取指定资源名称对应的字符串数组,使用Promise形式返回字符串数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | -------------------- | | ---------------------------------- | ------------ |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 | | Promise&lt;Array&lt;string&gt;&gt; | 资源名称对应的字符串数组 |
**错误码:** **错误码:**
...@@ -1128,27 +1148,28 @@ getMediaContentBase64(resId: number): Promise&lt;string&gt; ...@@ -1128,27 +1148,28 @@ getMediaContentBase64(resId: 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
try { try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then(value => { this.context.resourceManager.getStringArrayByName("test").then(value => {
let media = value; let strArray = value;
}).catch(error => { }).catch(error => {
console.log("getMediaContentBase64 promise error is " + error); console.log("getStringArrayByName promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>10+</sup> ### getPluralStringValueSync<sup>10+</sup>
getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt; getPluralStringValueSync(resId: number, num: number): string
用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。 根据指定数量获取指定ID字符串表示的单复数字符串,使用同步方式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -1157,13 +1178,13 @@ getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt; ...@@ -1157,13 +1178,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编码 | | string | 根据指定数量获取指定ID字符串表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1173,36 +1194,39 @@ getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt; ...@@ -1173,36 +1194,39 @@ 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
try { try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then(value => { this.context.resourceManager.getPluralStringValueSync($r('app.plural.test').id, 1);
let media = value;
}).catch(error => {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) { } catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getPluralStringValueSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentBase64<sup>9+</sup> ### getPluralStringValueSync<sup>10+</sup>
getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;): void getPluralStringValueSync(resource: Resource, num: number): string
用户获取指定resource对象对应的图片资源Base64编码,使用callback形式返回字符串。 根据指定数量获取指定resource对象表示的单复数字符串,使用同步方式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 | | num | number | 是 | 数量值 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| string | 根据指定数量获取指定resource对象表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1212,44 +1236,37 @@ getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;) ...@@ -1212,44 +1236,37 @@ 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 = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.media.test').id id: $r('app.plural.test').id
}; };
try { try {
this.context.resourceManager.getMediaContentBase64(resource, (error, value) => { this.context.resourceManager.getPluralStringValueSync(resource, 1);
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) { } catch (error) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getPluralStringValueSync 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(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。 根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **参数:**
**参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | -------- | --------------------------- | ---- | ------------------------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1259,48 +1276,43 @@ getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallba ...@@ -1259,48 +1276,43 @@ 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 = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaContentBase64(resource, 120, (error, value) => { this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => {
if (error != null) { if (error != null) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 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>9+</sup> ### getPluralStringValue<sup>9+</sup>
getMediaContentBase64(resource: Resource): Promise&lt;string&gt; getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
用户获取指定resource对象对应的图片资源Base64编码,使用Promise形式返回字符串。 根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ------------------------- | | --------------------- | ------------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 | | Promise&lt;string&gt; | 根据提供的数量获取对应ID字符串表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1310,30 +1322,26 @@ getMediaContentBase64(resource: Resource): Promise&lt;string&gt; ...@@ -1310,30 +1322,26 @@ getMediaContentBase64(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. |
**示例:** **示例:**
```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.getPluralStringValue($r("app.plural.test").id, 1).then(value => {
let media = value; let str = value;
}).catch(error => { }).catch(error => {
console.log("getMediaContentBase64 promise error is " + error); 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>10+</sup> ### getPluralStringValue<sup>9+</sup>
getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt; getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。 根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -1341,16 +1349,11 @@ getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt ...@@ -1341,16 +1349,11 @@ getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | --------------------------- | ---- | ------------------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -1360,159 +1363,140 @@ getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt ...@@ -1360,159 +1363,140 @@ getMediaContentBase64(resource: Resource, 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
let resource = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.media.test').id id: $r('app.plural.test').id
}; };
try { try {
this.context.resourceManager.getMediaContentBase64(resource, 120).then(value => { this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => {
let media = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } 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}.`);
} }
``` ```
### getConfiguration ### getPluralStringValue<sup>9+</sup>
getConfiguration(callback: AsyncCallback&lt;Configuration&gt;): void getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt;
用户获取设备的Configuration,使用callback形式返回Configuration对象 根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ------------------------- | | -------- | ---------------------- | ---- | ---- |
| callback | AsyncCallback&lt;[Configuration](#configuration)&gt; | 是 | 异步回调,用于返回设备的Configuration | | resource | [Resource](#resource9) | 是 | 资源信息 |
| num | number | 是 | 数量值 |
**示例:** **返回值:**
```ts
try {
this.context.resourceManager.getConfiguration((error, value) => {
if (error != null) {
console.error("getConfiguration callback error is " + error);
} else {
let direction = value.direction;
let locale = value.locale;
}
});
} catch (error) {
console.error("getConfiguration callback error is " + error);
}
```
### getConfiguration
getConfiguration(): Promise&lt;Configuration&gt;
用户获取设备的Configuration,使用Promise形式返回Configuration对象。 | 类型 | 说明 |
| --------------------- | ------------------------------ |
| Promise&lt;string&gt; | 根据提供的数量获取对应resource对象表示的单复数字符串 |
**系统能力**:SystemCapability.Global.ResourceManager **错误码:**
**返回值:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 类型 | 说明 | | 错误码ID | 错误信息 |
| ---------------------------------------- | ---------------- | | -------- | ---------------------------------------- |
| Promise&lt;[Configuration](#configuration)&gt; | 设备的Configuration | | 9001001 | If the resId invalid. |
| 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.plural.test').id
};
try { try {
this.context.resourceManager.getConfiguration().then(value => { this.context.resourceManager.getPluralStringValue(resource, 1).then(value => {
let direction = value.direction; let str = value;
let locale = value.locale;
}).catch(error => { }).catch(error => {
console.error("getConfiguration promise error is " + error); console.log("getPluralStringValue promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error("getConfiguration promise error is " + error); console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringByName<sup>9+</sup>
### getDeviceCapability getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;string&gt;): void
getDeviceCapability(callback: AsyncCallback&lt;DeviceCapability&gt;): void
用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象 根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ---------------------------- | | -------- | --------------------------- | ---- | ----------------------------- |
| callback | AsyncCallback&lt;[DeviceCapability](#devicecapability)&gt; | 是 | 异步回调,用于返回设备的DeviceCapability | | resName | string | 是 | 资源名称 |
| num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&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 ```ts
try { try {
this.context.resourceManager.getDeviceCapability((error, value) => { this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
if (error != null) { if (error != null) {
console.error("getDeviceCapability callback error is " + error); console.log("error is " + error);
} else { } else {
let screenDensity = value.screenDensity; let str = value;
let deviceType = value.deviceType;
} }
}); });
} catch (error) { } catch (error) {
console.error("getDeviceCapability callback error is " + error); console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringByName<sup>9+</sup>
### getDeviceCapability getPluralStringByName(resName: string, num: number): Promise&lt;string&gt;
getDeviceCapability(): Promise&lt;DeviceCapability&gt;
用户获取设备的DeviceCapability,使用Promise形式返回DeviceCapability对象 根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**返回值:** **参数:**
| 类型 | 说明 |
| ---------------------------------------- | ------------------- |
| Promise&lt;[DeviceCapability](#devicecapability)&gt; | 设备的DeviceCapability |
**示例:**
```ts
try {
this.context.resourceManager.getDeviceCapability().then(value => {
let screenDensity = value.screenDensity;
let deviceType = value.deviceType;
}).catch(error => {
console.error("getDeviceCapability promise error is " + error);
});
} catch (error) {
console.error("getDeviceCapability promise error is " + error);
}
```
### getPluralStringValue<sup>9+</sup>
getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void
根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| num | number | 是 | 数量值 |
**参数:** **返回值:**
| 参数名 | 类型 | 必填 | 说明 | | 类型 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------- | | --------------------- | ---------------------- |
| resId | number | 是 | 资源ID值 | | Promise&lt;string&gt; | 根据传入的数量值获取资源名称对应的字符串资源 |
| num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1520,46 +1504,43 @@ getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;stri ...@@ -1520,46 +1504,43 @@ getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;stri
| 错误码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.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => { this.context.resourceManager.getPluralStringByName("test", 1).then(value => {
if (error != null) { let str = value;
console.log("error is " + error); }).catch(error => {
} else { console.log("getPluralStringByName promise error is " + error);
let str = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContentSync<sup>10+</sup>
### getPluralStringValue<sup>9+</sup> getMediaContentSync(resId: number, density?: number): Uint8Array
getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串 用户获取指定资源ID对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ------------------------- | | -------- | ----------- |
| Promise&lt;string&gt; | 根据提供的数量获取对应ID字符串表示的单复数字符串 | | Uint8Array | 资源ID对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1569,38 +1550,44 @@ getPluralStringValue(resId: number, num: number): Promise&lt;string&gt; ...@@ -1569,38 +1550,44 @@ 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
try { try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then(value => { this.context.resourceManager.getMediaContentSync($r('app.media.test').id); // 默认屏幕密度
let str = value;
}).catch(error => {
console.log("getPluralStringValue promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getMediaContentSync failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getMediaContentSync($r('app.media.test').id, 120); // 指定屏幕密度
} catch (error) {
console.error(`getMediaContentSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringValue<sup>9+</sup> ### getMediaContentSync<sup>10+</sup>
getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void getMediaContentSync(resource: Resource, density?: number): Uint8Array
根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串 用户获取指定resource对象对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------------ | | ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| num | number | 是 | 数量值 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Uint8Array | resource对象对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1610,50 +1597,41 @@ getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt ...@@ -1610,50 +1597,41 @@ 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 = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.plural.test').id id: $r('app.media.test').id
}; };
try { try {
this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => { this.context.resourceManager.getMediaContentSync(resource); // 默认屏幕密度
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) { } catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getMediaContentSync failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getMediaContentSync(resource, 120); // 指定屏幕密度
} catch (error) {
console.error(`getMediaContentSync 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(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串 用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 | | callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------------ |
| Promise&lt;string&gt; | 根据提供的数量获取对应resource对象表示的单复数字符串 |
**错误码:** **错误码:**
...@@ -1663,41 +1641,37 @@ getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt; ...@@ -1663,41 +1641,37 @@ 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 = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.plural.test').id
};
try { try {
this.context.resourceManager.getPluralStringValue(resource, 1).then(value => { this.context.resourceManager.getMediaContent($r('app.media.test').id, (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}.`);
} }
``` ```
### getMediaContent<sup>10+</sup>
### getRawFileContent<sup>9+</sup> getMediaContent(resId: number, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- | | -------- | ------------------------------- | ---- | ------------------ |
| path | string | 是 | rawfile文件路径 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的rawfile文件内容 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1705,42 +1679,43 @@ getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void ...@@ -1705,42 +1679,43 @@ 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
try { try {
this.context.resourceManager.getRawFileContent("test.xml", (error, value) => { this.context.resourceManager.getMediaContent($r('app.media.test').id, 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 rawFile = value; let media = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getRawFileContent<sup>9+</sup> ### getMediaContent<sup>9+</sup>
getRawFileContent(path: string): Promise&lt;Uint8Array&gt; getMediaContent(resId: number): Promise&lt;Uint8Array&gt;
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。 用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- | | ----- | ------ | ---- | ----- |
| path | string | 是 | rawfile文件路径 | | resId | number | 是 | 资源ID值 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | ----------- | | ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | rawfile文件内容 | | Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1748,36 +1723,42 @@ getRawFileContent(path: string): Promise&lt;Uint8Array&gt; ...@@ -1748,36 +1723,42 @@ getRawFileContent(path: string): Promise&lt;Uint8Array&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 { try {
this.context.resourceManager.getRawFileContent("test.xml").then(value => { this.context.resourceManager.getMediaContent($r('app.media.test').id).then(value => {
let rawFile = value; let media = value;
}).catch(error => { }).catch(error => {
console.log("getRawFileContent promise error is " + error); console.log("getMediaContent promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaContent<sup>10+</sup>
### getRawFd<sup>9+</sup> getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt;
getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | -------------------------------- | | ----- | ------ | ---- | ----- |
| path | string | 是 | rawfile文件路径 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | 是 | 异步回调,用于返回获取的rawfile文件的descriptor | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1785,44 +1766,38 @@ getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void ...@@ -1785,44 +1766,38 @@ getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&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
try { try {
this.context.resourceManager.getRawFd("test.xml", (error, value) => { this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then(value => {
if (error != null) { let media = value;
console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); }).catch(error => {
} else { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
let fd = value.fd;
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(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getRawFd<sup>9+</sup> ### getMediaContent<sup>9+</sup>
getRawFd(path: string): Promise&lt;RawFileDescriptor&gt; getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回 用户获取指定resource对象对应的媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
**返回值:** **参数:**
| 类型 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ---------------------------------------- | ------------------- | | -------- | ------------------------------- | ---- | ------------------ |
| Promise&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | rawfile文件descriptor | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1830,37 +1805,46 @@ getRawFd(path: string): Promise&lt;RawFileDescriptor&gt; ...@@ -1830,37 +1805,46 @@ getRawFd(path: string): Promise&lt;RawFileDescriptor&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
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getRawFd("test.xml").then(value => { this.context.resourceManager.getMediaContent(resource, (error, value) => {
let fd = value.fd; if (error != null) {
let offset = value.offset; console.log("error is " + error);
let length = value.length; } else {
}).catch(error => { let media = value;
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(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getRawFileList<sup>10+</sup> ### getMediaContent<sup>10+</sup>
getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): void; getMediaContent(resource: Resource, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下文件夹及文件列表,使用callback形式返回文件列表的字符串数组。 用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- | | -------- | ------------------------------- | ---- | ------------------ |
| path | string | 是 | rawfile文件夹路径 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Array\<string\>&gt; | 是 | 异步回调,用于返回获取rawfile文件目录下的文件列表 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1868,42 +1852,50 @@ getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): vo ...@@ -1868,42 +1852,50 @@ getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): vo
| 错误码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根目录下的文件列表 let resource = {
this.context.resourceManager.getRawFileList("", (error, value) => { bundleName: "com.example.myapplication",
if (error != null) { moduleName: "entry",
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); id: $r('app.media.test').id
} else { };
let rawFile = value; try {
} this.context.resourceManager.getMediaContent(resource, 120, (error, value) => {
if (error != null) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getRawFileList<sup>10+</sup> ### getMediaContent<sup>9+</sup>
getRawFileList(path: string): Promise&lt;Array\<string\>&gt; getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt;
用户获取resources/rawfile目录下文件夹及文件列表,使用Promise形式返回文件列表字符串数组。 用户获取指定resource对象对应的媒体文件内容,使用Promise形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- | | -------- | ---------------------- | ---- | ---- |
| path | string | 是 | rawfile文件夹路径 | | resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | ----------- | | ------------------------- | ------------------- |
| Promise&lt;Array\<string\>&gt; | rawfile文件目录下的文件列表 | | Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1911,35 +1903,49 @@ getRawFileList(path: string): Promise&lt;Array\<string\>&gt; ...@@ -1911,35 +1903,49 @@ 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根目录下的文件列表 let resource = {
this.context.resourceManager.getRawFileList("").then(value => { bundleName: "com.example.myapplication",
let rawFile = value; moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaContent(resource).then(value => {
let media = value;
}).catch(error => { }).catch(error => {
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); console.log("getMediaContent promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### closeRawFd<sup>9+</sup> ### getMediaContent<sup>10+</sup>
closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt;
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回 用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----------- | | -------- | ---------------------- | ---- | ---- |
| path | string | 是 | rawfile文件路径 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;void&gt; | 是 | 异步回调 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------------- |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -1947,41 +1953,41 @@ closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -1947,41 +1953,41 @@ 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
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.closeRawFd("test.xml", (error, value) => { this.context.resourceManager.getMediaContent(resource, 120).then(value => {
if (error != null) { let media = value;
console.log("error is " + error); }).catch(error => {
} console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
}); });
} catch (error) { } catch (error) {
console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### closeRawFd<sup>9+</sup> ### getMediaByName<sup>9+</sup>
closeRawFd(path: string): Promise&lt;void&gt; getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回 用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- | | -------- | ------------------------------- | ---- | ------------------ |
| path | string | 是 | rawfile文件路径 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ---- |
| Promise&lt;void&gt; | 无返回值 |
**错误码:** **错误码:**
...@@ -1989,52 +1995,39 @@ closeRawFd(path: string): Promise&lt;void&gt; ...@@ -1989,52 +1995,39 @@ closeRawFd(path: string): Promise&lt;void&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.closeRawFd("test.xml").then(value => { this.context.resourceManager.getMediaByName("test", (error, value) => {
let result = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.log("closeRawFd promise error is " + error); } else {
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### release<sup>7+</sup>
release()
用户释放创建的resourceManager。
**系统能力**:SystemCapability.Global.ResourceManager
**示例:**
```ts
try {
this.context.resourceManager.release();
} catch (error) {
console.error("release error is " + error);
} }
``` ```
### getStringByName<sup>9+</sup> ### getMediaByName<sup>10+</sup>
getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void getMediaByName(resName: string, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取指定资源名称对应的字符串,使用callback形式返回字符串 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- | | -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -2044,32 +2037,31 @@ getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void ...@@ -2044,32 +2037,31 @@ getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringByName("test", (error, value) => { this.context.resourceManager.getMediaByName("test", 120, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} 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 getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringByName<sup>9+</sup> ### getMediaByName<sup>9+</sup>
getStringByName(resName: string): Promise&lt;string&gt; getMediaByName(resName: string): Promise&lt;Uint8Array&gt;
用户获取指定资源名称对应的字符串,使用Promise形式返回字符串 用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ------- | ------ | ---- | ---- |
...@@ -2077,9 +2069,9 @@ getStringByName(resName: string): Promise&lt;string&gt; ...@@ -2077,9 +2069,9 @@ getStringByName(resName: string): Promise&lt;string&gt;
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ---------- | | ------------------------- | ------------- |
| Promise&lt;string&gt; | 资源名称对应的字符串 | | Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -2089,35 +2081,40 @@ getStringByName(resName: string): Promise&lt;string&gt; ...@@ -2089,35 +2081,40 @@ getStringByName(resName: string): Promise&lt;string&gt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringByName("test").then(value => { this.context.resourceManager.getMediaByName("test").then(value => {
let string = value; let media = value;
}).catch(error => { }).catch(error => {
console.log("getStringByName promise error is " + error); console.log("getMediaByName promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`)
} }
``` ```
### getStringArrayByName<sup>9+</sup> ### getMediaByName<sup>10+</sup>
getStringArrayByName(resName: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void getMediaByName(resName: string, density: number): Promise&lt;Uint8Array&gt;
用户获取指定资源名称对应的字符串数组,使用callback形式返回字符串数组。 用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- | | ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 | | resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -2127,42 +2124,40 @@ getStringArrayByName(resName: string, callback: AsyncCallback&lt;Array&lt;string ...@@ -2127,42 +2124,40 @@ getStringArrayByName(resName: string, callback: AsyncCallback&lt;Array&lt;string
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringArrayByName("test", (error, value) => { this.context.resourceManager.getMediaByName("test", 120).then(value => {
if (error != null) { let media = value;
console.log("error is " + error); }).catch(error => {
} else { console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
let strArray = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringArrayByName<sup>9+</sup> ### getMediaContentBase64Sync<sup>10+</sup>
getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt; getMediaContentBase64Sync(resId: number, density?: number): string
用户获取指定资源名称对应的字符串数组,使用Promise形式返回字符串数组 用户获取指定资源ID对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ---------------------------------- | ------------ | | -------- | ----------- |
| Promise&lt;Array&lt;string&gt;&gt; | 资源名称对应的字符串数组 | | string | 资源ID对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2170,37 +2165,46 @@ getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt; ...@@ -2170,37 +2165,46 @@ 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 { try {
this.context.resourceManager.getStringArrayByName("test").then(value => { this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id); // 默认屏幕密度
let strArray = value;
}).catch(error => {
console.log("getStringArrayByName promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id, 120); // 指定屏幕密度
} catch (error) {
console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaByName<sup>9+</sup> ### getMediaContentBase64Sync<sup>10+</sup>
getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void getMediaContentBase64Sync(resource: Resource, density?: number): string
用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组 用户获取指定resource对象对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ | | ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| string | resource对象对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2208,39 +2212,43 @@ getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void ...@@ -2208,39 +2212,43 @@ getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&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. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaByName("test", (error, value) => { this.context.resourceManager.getMediaContentBase64Sync(resource); // 默认屏幕密度
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) { } catch (error) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getMediaContentBase64Sync(resource, 120); // 指定屏幕密度
} catch (error) {
console.error(`getMediaContentBase64Sync 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(resId: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组 用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:** Content
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ | | -------- | --------------------------- | ---- | ------------------------ |
| resName | string | 是 | 资源名称 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:** **错误码:**
...@@ -2248,43 +2256,39 @@ getMediaByName(resName: string, density: number, callback: AsyncCallback&lt;Uint ...@@ -2248,43 +2256,39 @@ getMediaByName(resName: string, density: number, callback: AsyncCallback&lt;Uint
| 错误码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
try { try {
this.context.resourceManager.getMediaByName("test", 120, (error, value) => { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => {
if (error != null) { if (error != null) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); console.log("error is " + error);
} 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>9+</sup> ### getMediaContentBase64<sup>10+</sup>
getMediaByName(resName: string): Promise&lt;Uint8Array&gt; getMediaContentBase64(resId: number, density: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组 用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | -------- | --------------------------- | ---- | ------------------------ |
| resName | string | 是 | 资源名称 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
| 类型 | 说明 |
| ------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
**错误码:** **错误码:**
...@@ -2292,42 +2296,43 @@ getMediaByName(resName: string): Promise&lt;Uint8Array&gt; ...@@ -2292,42 +2296,43 @@ 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
try { try {
this.context.resourceManager.getMediaByName("test").then(value => { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => {
if (error != null) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value; let media = value;
}).catch(error => { }
console.log("getMediaByName promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise 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): Promise&lt;Uint8Array&gt; getMediaContentBase64(resId: number): Promise&lt;string&gt;
用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组 用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------------------------- | ------------- | | --------------------- | -------------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 | | Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2335,36 +2340,42 @@ getMediaByName(resName: string, density: number): Promise&lt;Uint8Array&gt; ...@@ -2335,36 +2340,42 @@ 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
try { try {
this.context.resourceManager.getMediaByName("test", 120).then(value => { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then(value => {
let media = value; let media = value;
}).catch(error => { }).catch(error => {
console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); 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}.`);
} }
``` ```
### getMediaBase64ByName<sup>9+</sup> ### getMediaContentBase64<sup>10+</sup>
getMediaBase64ByName(resName: string, callback: AsyncCallback&lt;string&gt;): void getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt;
用户获取指定资源名称对应的图片资源Base64编码,使用callback形式返回字符串。 用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称 | | resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2372,38 +2383,37 @@ getMediaBase64ByName(resName: string, callback: AsyncCallback&lt;string&gt;): vo ...@@ -2372,38 +2383,37 @@ getMediaBase64ByName(resName: string, callback: AsyncCallback&lt;string&gt;): vo
| 错误码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
try { try {
this.context.resourceManager.getMediaBase64ByName("test", (error, value) => { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then(value => {
if (error != null) { let media = value;
console.log("error is " + error); }).catch(error => {
} else { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaBase64ByName<sup>10+</sup> ### getMediaContentBase64<sup>9+</sup>
getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback&lt;string&gt;): void getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。 用户获取指定resource对象对应的图片资源Base64编码,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ | | -------- | --------------------------- | ---- | ------------------------ |
| resName | string | 是 | 资源名称 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2412,43 +2422,46 @@ getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback&l ...@@ -2412,43 +2422,46 @@ getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback&l
| 错误码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
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaBase64ByName("test", 120, (error, value) => { this.context.resourceManager.getMediaContentBase64(resource, (error, value) => {
if (error != null) { if (error != null) {
console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); 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 getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaBase64ByName<sup>9+</sup> ### getMediaContentBase64<sup>10+</sup>
getMediaBase64ByName(resName: string): Promise&lt;string&gt; getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的图片资源Base64编码,使用Promise形式返回字符串。 用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:** **参数:**
| 类型 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| --------------------- | ------------------- | | -------- | --------------------------- | ---- | ------------------------ |
| Promise&lt;string&gt; | 资源名称对应的图片资源Base64编码 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2456,42 +2469,50 @@ getMediaBase64ByName(resName: string): Promise&lt;string&gt; ...@@ -2456,42 +2469,50 @@ getMediaBase64ByName(resName: string): 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. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaBase64ByName("test").then(value => { this.context.resourceManager.getMediaContentBase64(resource, 120, (error, value) => {
if (error != null) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value; let media = value;
}).catch(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(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getMediaBase64ByName<sup>10+</sup> ### getMediaContentBase64<sup>9+</sup>
getMediaBase64ByName(resName: string, density: number): Promise&lt;string&gt; getMediaContentBase64(resource: Resource): Promise&lt;string&gt;
用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。 用户获取指定resource对象对应的图片资源Base64编码,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
| resName | string | 是 | 资源名称 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| --------------------- | ------------------- | | --------------------- | ------------------------- |
| Promise&lt;string&gt; | 资源名称对应的图片资源Base64编码 | | Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2499,37 +2520,49 @@ getMediaBase64ByName(resName: string, density: number): Promise&lt;string&gt; ...@@ -2499,37 +2520,49 @@ getMediaBase64ByName(resName: string, density: 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. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getMediaBase64ByName("test", 120).then(value => { this.context.resourceManager.getMediaContentBase64(resource).then(value => {
let media = value; let media = value;
}).catch(error => { }).catch(error => {
console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.log("getMediaContentBase64 promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringByName<sup>9+</sup> ### getMediaContentBase64<sup>10+</sup>
getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;string&gt;): void getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt;
根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串。 用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ----------------------------- | | -------- | ---------------------- | ---- | ---- |
| resName | string | 是 | 资源名称 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| num | number | 是 | 数量值 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据传入的数量值获取资源名称对应的字符串资源 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2537,46 +2570,41 @@ getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;s ...@@ -2537,46 +2570,41 @@ getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;s
| 错误码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
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try { try {
this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => { this.context.resourceManager.getMediaContentBase64(resource, 120).then(value => {
if (error != null) { let media = value;
console.log("error is " + error); }).catch(error => {
} else { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
let str = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getPluralStringByName<sup>9+</sup> ### getMediaBase64ByName<sup>9+</sup>
getPluralStringByName(resName: string, num: number): Promise&lt;string&gt; getMediaBase64ByName(resName: string, callback: AsyncCallback&lt;string&gt;): void
根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串。 用户获取指定资源名称对应的图片资源Base64编码,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | -------- | --------------------------- | ---- | ------------------------ |
| resName | string | 是 | 资源名称 | | resName | string | 是 | 资源名称 |
| num | number | 是 | 数量值 | | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------------- |
| Promise&lt;string&gt; | 根据传入的数量值获取资源名称对应的字符串资源 |
**错误码:** **错误码:**
...@@ -2586,40 +2614,37 @@ getPluralStringByName(resName: string, num: number): Promise&lt;string&gt; ...@@ -2586,40 +2614,37 @@ getPluralStringByName(resName: string, num: number): Promise&lt;string&gt;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getPluralStringByName("test", 1).then(value => { this.context.resourceManager.getMediaBase64ByName("test", (error, value) => {
let str = value; if (error != null) {
}).catch(error => { console.log("error is " + error);
console.log("getPluralStringByName promise error is " + error); } else {
let media = value;
}
}); });
} catch (error) { } catch (error) {
console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>9+</sup> ### getMediaBase64ByName<sup>10+</sup>
getStringSync(resId: number): string getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源ID对应的字符串,使用同步方式返回字符串。 用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | -------- | --------------------------- | ---- | ------------------------ |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** | callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
| 类型 | 说明 |
| ------ | ----------- |
| string | 资源ID值对应的字符串 |
**错误码:** **错误码:**
...@@ -2627,81 +2652,86 @@ getStringSync(resId: number): string ...@@ -2627,81 +2652,86 @@ 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.getMediaBase64ByName("test", 120, (error, value) => {
if (error != null) {
console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) { } catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>10+</sup> ### getMediaBase64ByName<sup>9+</sup>
getStringSync(resId: number, ...args: Array<string | number>): string getMediaBase64ByName(resName: string): Promise&lt;string&gt;
用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。 用户获取指定资源名称对应的图片资源Base64编码,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ------- | ------ | ---- | ---- |
| resId | number | 是 | 资源ID值 | | resName | string | 是 | 资源名称 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------------------------- | | --------------------- | ------------------- |
| string | 资源ID值对应的格式化字符串| | Promise&lt;string&gt; | 资源名称对应的图片资源Base64编码 |
**错误码:** **错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码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. |
| 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.getMediaBase64ByName("test").then(value => {
let media = value;
}).catch(error => {
console.log("getMediaBase64ByName promise error is " + error);
});
} catch (error) { } catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>9+</sup> ### getMediaBase64ByName<sup>10+</sup>
getStringSync(resource: Resource): string getMediaBase64ByName(resName: string, density: number): Promise&lt;string&gt;
用户获取指定resource对象对应的字符串,使用同步方式返回字符串。 用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | ------- | ------ | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------------- | | --------------------- | ------------------- |
| string | resource对象对应的字符串 | | Promise&lt;string&gt; | 资源名称对应的图片资源Base64编码 |
**错误码:** **错误码:**
...@@ -2709,46 +2739,42 @@ getStringSync(resource: Resource): string ...@@ -2709,46 +2739,42 @@ getStringSync(resource: Resource): 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
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
try { try {
this.context.resourceManager.getStringSync(resource); this.context.resourceManager.getMediaBase64ByName("test", 120).then(value => {
let media = value;
}).catch(error => {
console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) { } catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getStringSync<sup>10+</sup> ### getDrawableDescriptor<sup>10+</sup>
getStringSync(resource: Resource, ...args: Array<string | number>): string getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;
用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串 用户获取指定资源ID对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。 **参数:**
**参数:** | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| 参数名 | 类型 | 必填 | 说明 | | resId | number | 是 | 资源ID值 |
| -------- | ---------------------- | ---- | ---- | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------------------------- | | ------ | ---------- |
| string | resource对象对应的格式化字符串| | DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:** **错误码:**
...@@ -2758,42 +2784,43 @@ getStringSync(resource: Resource, ...args: Array<string | number>): string ...@@ -2758,42 +2784,43 @@ getStringSync(resource: Resource, ...args: Array<string | number>): string
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 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. |
| 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.getDrawableDescriptor($r('app.media.icon').id);
} catch (error) { } catch (error) {
console.error(`getStringSync 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}.`);
}
```
### getStringByNameSync<sup>9+</sup> ### getDrawableDescriptor<sup>10+</sup>
getStringByNameSync(resName: string): string getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
用户获取指定资源名称对应的字符串,使用同步方式返回字符串 用户获取指定resource对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **模型约束**:此接口仅可在Stage模型下使用。
| 参数名 | 类型 | 必填 | 说明 | **参数:**
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------- | | ------- | ----------------- |
| string | 资源名称对应的字符串 | | DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:** **错误码:**
...@@ -2801,39 +2828,48 @@ getStringByNameSync(resName: string): string ...@@ -2801,39 +2828,48 @@ 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. |
**示例:** **示例:**
```ts ```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.icon').id
};
try { try {
this.context.resourceManager.getStringByNameSync("test"); this.context.resourceManager.getDrawableDescriptor(resource);
} catch (error) { } catch (error) {
console.error(`getStringByNameSync 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}.`);
} }
``` ```
### getStringByNameSync<sup>10+</sup> ### getDrawableDescriptorByName<sup>10+</sup>
getStringByNameSync(resName: string, ...args: Array<string | number>): string getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor;
用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串 用户获取指定资源名称对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 | | resName | string | 是 | 资源名称 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串| | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------------------------- | | ------ | --------- |
| string | 资源名称对应的格式化字符串| | DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:** **错误码:**
...@@ -2843,17 +2879,20 @@ getStringByNameSync(resName: string, ...args: Array<string | number>): string ...@@ -2843,17 +2879,20 @@ getStringByNameSync(resName: string, ...args: Array<string | number>): string
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001008 | If the resource obtained by resName formatting error. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78); this.context.resourceManager.getDrawableDescriptorByName('icon');
} catch (error) { } catch (error) {
console.error(`getStringByNameSync 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}.`);
}
```
### getBoolean<sup>9+</sup> ### getBoolean<sup>9+</sup>
...@@ -2928,9 +2967,9 @@ getBoolean(resource: Resource): boolean ...@@ -2928,9 +2967,9 @@ getBoolean(resource: Resource): boolean
**示例:** **示例:**
```ts ```ts
let resource = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.boolean.boolean_test').id id: $r('app.boolean.boolean_test').id
}; };
try { try {
this.context.resourceManager.getBoolean(resource); this.context.resourceManager.getBoolean(resource);
...@@ -3058,9 +3097,9 @@ getNumber(resource: Resource): number ...@@ -3058,9 +3097,9 @@ getNumber(resource: Resource): number
**示例:** **示例:**
```ts ```ts
let resource = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.integer.integer_test').id id: $r('app.integer.integer_test').id
}; };
try { try {
this.context.resourceManager.getNumber(resource);// integer对应返回的是原数值, float对应返回的是真实像素点值 this.context.resourceManager.getNumber(resource);// integer对应返回的是原数值, float对应返回的是真实像素点值
...@@ -3114,11 +3153,11 @@ getNumberByName(resName: string): number ...@@ -3114,11 +3153,11 @@ getNumberByName(resName: string): number
} }
``` ```
### getDrawableDescriptor<sup>10+</sup> ### getColorSync<sup>10+</sup>
getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor; getColorSync(resId: number) : number;
用户获取指定资源ID对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示 用户获取指定资源ID对应的颜色值,使用同步方式返回其对应的颜色值
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -3127,13 +3166,12 @@ getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor; ...@@ -3127,13 +3166,12 @@ getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- | | ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 | | resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | ---------- | | ------ | ----------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | | number | 资源ID值对应的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -3143,26 +3181,22 @@ getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor; ...@@ -3143,26 +3181,22 @@ getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 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.getDrawableDescriptor($r('app.media.icon').id); this.context.resourceManager.getColorSync($r('app.color.test').id);
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
}
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(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getDrawableDescriptor<sup>10+</sup> ### getColorSync<sup>10+</sup>
getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor; getColorSync(resource: Resource): number
用户获取指定resource对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示 用户获取指定resource对象对应的颜色值,使用同步方式返回其对应的颜色值
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -3173,13 +3207,12 @@ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor; ...@@ -3173,13 +3207,12 @@ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- | | -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 | | resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------- | ----------------- | | ------ | ---------------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | | number | resource对象对应的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -3189,31 +3222,27 @@ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor; ...@@ -3189,31 +3222,27 @@ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 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 = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.media.icon').id id: $r('app.color.test').id
}; };
try { try {
this.context.resourceManager.getDrawableDescriptor(resource); this.context.resourceManager.getColorSync(resource);
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getDrawableDescriptor(resource, 120);
} catch (error) { } catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getDrawableDescriptorByName<sup>10+</sup> ### getColorByNameSync<sup>10+</sup>
getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor; getColorByNameSync(resName: string) : number;
用户获取指定资源名称对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示 用户获取指定资源名称对应的颜色值,使用同步方式返回其对应的颜色值
**系统能力**:SystemCapability.Global.ResourceManager **系统能力**:SystemCapability.Global.ResourceManager
...@@ -3222,13 +3251,12 @@ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescript ...@@ -3222,13 +3251,12 @@ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescript
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- | | ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 | | resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| ------ | --------- | | ------ | ---------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | | number | 资源名称对应的颜色值(十进制) |
**错误码:** **错误码:**
...@@ -3238,18 +3266,14 @@ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescript ...@@ -3238,18 +3266,14 @@ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescript
| -------- | ---------------------------------------- | | -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. | | 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. | | 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```ts ```ts
try { try {
this.context.resourceManager.getDrawableDescriptorByName('icon'); this.context.resourceManager.getColorByNameSync("test");
} catch (error) {
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getDrawableDescriptorByName('icon', 120);
} catch (error) { } catch (error) {
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`); console.error(`getColorByNameSync failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
...@@ -3280,17 +3304,17 @@ getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void; ...@@ -3280,17 +3304,17 @@ getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void;
**示例Stage:** **示例Stage:**
```ts ```ts
try { try {
this.context.resourceManager.getColor($r('app.color.test').id, (error, value) => { this.context.resourceManager.getColor($r('app.color.test').id, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let str = value; let str = value;
} }
}); });
} catch (error) { } catch (error) {
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`);
} }
``` ```
### getColor<sup>10+</sup> ### getColor<sup>10+</sup>
...@@ -3327,9 +3351,9 @@ getColor(resId: number): Promise&lt;number&gt; ...@@ -3327,9 +3351,9 @@ getColor(resId: number): Promise&lt;number&gt;
```ts ```ts
try { try {
this.context.resourceManager.getColor($r('app.color.test').id).then(value => { this.context.resourceManager.getColor($r('app.color.test').id).then(value => {
let str = value; let str = value;
}).catch(error => { }).catch(error => {
console.log("getColor promise error is " + error); console.log("getColor promise error is " + error);
}); });
} catch (error) { } catch (error) {
console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`); console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`);
...@@ -3366,44 +3390,373 @@ getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void; ...@@ -3366,44 +3390,373 @@ getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void;
**示例:** **示例:**
```ts ```ts
let resource = { let resource = {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
moduleName: "entry", moduleName: "entry",
id: $r('app.color.test').id id: $r('app.color.test').id
}; };
try { try {
this.context.resourceManager.getColor(resource, (error, value) => { this.context.resourceManager.getColor(resource, (error, value) => {
if (error != null) { if (error != null) {
console.log("error is " + error); console.log("error is " + error);
} else { } else {
let str = value; let str = value;
} }
});
} catch (error) {
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColor<sup>10+</sup>
getColor(resource: Resource): Promise&lt;number&gt;;
用户获取指定resource对象对应的颜色值,使用Promise形式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------- |
| Promise&lt;number&gt; | resource对象对应的颜色值(十进制) |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../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
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try {
this.context.resourceManager.getColor(resource).then(value => {
let str = value;
}).catch(error => {
console.log("getColor promise error is " + error);
});
} catch (error) {
console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColorByName<sup>10+</sup>
getColorByName(resName: string, callback: AsyncCallback&lt;number&gt;): void
用户获取指定资源名称对应的颜色值,使用callback形式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;number&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.getColorByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let string = value;
}
});
} catch (error) {
console.error(`callback getColorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColorByName<sup>10+</sup>
getColorByName(resName: string): Promise&lt;number&gt;
用户获取指定资源名称对应的颜色值,使用Promise形式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------- |
| Promise&lt;number&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.getColorByName("test").then(value => {
let string = value;
}).catch(error => {
console.log("getColorByName promise error is " + error);
});
} catch (error) {
console.error(`promise getColorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getRawFileContent<sup>9+</sup>
getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的rawfile文件内容 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
try {
this.context.resourceManager.getRawFileContent("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let rawFile = value;
}
});
} catch (error) {
console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getRawFileContent<sup>9+</sup>
getRawFileContent(path: string): Promise&lt;Uint8Array&gt;
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | rawfile文件内容 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
try {
this.context.resourceManager.getRawFileContent("test.xml").then(value => {
let rawFile = value;
}).catch(error => {
console.log("getRawFileContent promise error is " + error);
});
} catch (error) {
console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getRawFileList<sup>10+</sup>
getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): void;
用户获取resources/rawfile目录下文件夹及文件列表,使用callback形式返回文件列表的字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件夹路径 |
| callback | AsyncCallback&lt;Array\<string\>&gt; | 是 | 异步回调,用于返回获取rawfile文件目录下的文件列表 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
try { // 传入""表示获取rawfile根目录下的文件列表
this.context.resourceManager.getRawFileList("", (error, value) => {
if (error != null) {
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let rawFile = value;
}
});
} catch (error) {
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getRawFileList<sup>10+</sup>
getRawFileList(path: string): Promise&lt;Array\<string\>&gt;
用户获取resources/rawfile目录下文件夹及文件列表,使用Promise形式返回文件列表字符串数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件夹路径 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Promise&lt;Array\<string\>&gt; | rawfile文件目录下的文件列表 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
try { // 传入""表示获取rawfile根目录下的文件列表
this.context.resourceManager.getRawFileList("").then(value => {
let rawFile = value;
}).catch(error => {
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getRawFd<sup>9+</sup>
getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | 是 | 异步回调,用于返回获取的rawfile文件的descriptor |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
try {
this.context.resourceManager.getRawFd("test.xml", (error, value) => {
if (error != null) {
console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`);
} else {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}
}); });
} catch (error) { } 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 +3764,37 @@ getColor(resource: Resource): Promise&lt;number&gt;; ...@@ -3411,42 +3764,37 @@ 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 = {
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.xml").then(value => {
let str = value; let fd = value.fd;
let offset = value.offset;
let length = value.length;
}).catch(error => { }).catch(error => {
console.log("getColor promise error is " + error); 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 +3802,40 @@ getColorByName(resName: string, callback: AsyncCallback&lt;number&gt;): void ...@@ -3454,44 +3802,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.xml", (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 +3843,151 @@ getColorByName(resName: string): Promise&lt;number&gt; ...@@ -3499,144 +3843,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.xml").then(value => {
let string = value; let result = value;
}).catch(error => { }).catch(error => {
console.log("getColorByName promise error is " + error); console.log("closeRawFd 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) | 是 | 资源信息 | 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
| ------ | ---------------- |
| number | resource对象对应的颜色值(十进制) |
**错误码:** 用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象。
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 **系统能力**:SystemCapability.Global.ResourceManager
| 错误码ID | 错误信息 | **参数:**
| -------- | ---------------------------------------- |
| 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
try {
this.context.resourceManager.getDeviceCapability().then(value => {
let screenDensity = value.screenDensity;
let deviceType = value.deviceType;
}).catch(error => {
console.error("getDeviceCapability promise error is " + error);
});
} catch (error) {
console.error("getDeviceCapability promise error is " + error);
}
```
| 类型 | 说明 | ### release<sup>7+</sup>
| ------ | ---------- |
| number | 资源名称对应的颜色值(十进制) |
**错误码:** release()
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md) 用户释放创建的resourceManager
| 错误码ID | 错误信息 | **系统能力**:SystemCapability.Global.ResourceManager
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**示例:** **示例:**
```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 +4017,9 @@ addResource(path: string) : void; ...@@ -3666,9 +4017,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 +4049,9 @@ removeResource(path: string) : void; ...@@ -3698,9 +4049,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.resmgr.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 +4072,7 @@ getString(resId: number, callback: AsyncCallback&lt;string&gt;): void ...@@ -3721,7 +4072,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) => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册