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

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

Merge pull request !22456 from zt147369/master
......@@ -150,7 +150,6 @@ getResourceManager(bundleName: string): Promise<ResourceManager>
});
```
## resourceManager.getSystemResourceManager<sup>10+</sup>
getSystemResourceManager(): ResourceManager
......@@ -177,16 +176,16 @@ getSystemResourceManager(): ResourceManager
```js
import resourceManager from '@ohos.resourceManager';
try {
try {
let systemResourceManager = resourceManager.getSystemResourceManager();
systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then(value => {
let str = value;
}).catch(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}.`);
}
}
```
......@@ -301,20 +300,25 @@ try {
>
> - 资源文件在工程的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
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| string | 资源ID值对应的字符串 |
**错误码:**
......@@ -322,31 +326,24 @@ getStringValue(resId: number, callback: AsyncCallback&lt;string&gt;): void
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the module resId invalid. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例Stage:**
**示例:**
```ts
try {
this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
this.context.resourceManager.getStringSync($r('app.string.test').id);
} 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>
getStringValue(resId: number): Promise&lt;string&gt;
getStringSync(resId: number, ...args: Array<string | number>): string
用户获取指定资源ID对应的字符串,使用Promise形式返回字符串。
用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**系统能力**:SystemCapability.Global.ResourceManager
......@@ -355,42 +352,39 @@ getStringValue(resId: number): Promise&lt;string&gt;
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| 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)
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| -------- | ----------------------------------------------- |
| 9001001 | If the resId invalid. |
| 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
try {
this.context.resourceManager.getStringValue($r('app.string.test').id).then(value => {
let str = value;
}).catch(error => {
console.log("getStringValue promise error is " + error);
});
this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78);
} 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>
getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
getStringSync(resource: Resource): string
用户获取指定resource对象对应的字符串,使用callback形式返回字符串。
用户获取指定resource对象对应的字符串,使用同步方式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
......@@ -399,9 +393,14 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------------- |
| string | resource对象对应的字符串 |
**错误码:**
......@@ -421,25 +420,17 @@ getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
id: $r('app.string.test').id
};
try {
this.context.resourceManager.getStringValue(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
this.context.resourceManager.getStringSync(resource);
} 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>
getStringValue(resource: Resource): Promise&lt;string&gt;
getStringSync(resource: Resource, ...args: Array<string | number>): string
用户获取指定resource对象对应的字符串,使用Promise形式返回字符串。
用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**系统能力**:SystemCapability.Global.ResourceManager
......@@ -450,12 +441,13 @@ getStringValue(resource: Resource): Promise&lt;string&gt;
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> %d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------- |
| Promise&lt;string&gt; | resource对象对应的字符串 |
| ------ | ---------------------------- |
| string | resource对象对应的格式化字符串|
**错误码:**
......@@ -466,6 +458,7 @@ getStringValue(resource: Resource): Promise&lt;string&gt;
| 9001001 | If the resId invalid. |
| 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
......@@ -475,31 +468,31 @@ getStringValue(resource: Resource): Promise&lt;string&gt;
id: $r('app.string.test').id
};
try {
this.context.resourceManager.getStringValue(resource).then(value => {
let str = value;
}).catch(error => {
console.log("getStringValue promise error is " + error);
});
this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78);
} 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}.`);
}
```
### getStringByNameSync<sup>9+</sup>
### getStringArrayValue<sup>9+</sup>
getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
getStringByNameSync(resName: string): string
用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组
用户获取指定资源名称对应的字符串,使用同步方式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| string | 资源名称对应的字符串 |
**错误码:**
......@@ -507,45 +500,39 @@ getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 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.getStringArrayValue($r('app.strarray.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
this.context.resourceManager.getStringByNameSync("test");
} 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>
getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt;
getStringByNameSync(resName: string, ...args: Array<string | number>): string
用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组
用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串
**系统能力**: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;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001003 | If the resName invalid. |
| 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
try {
this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then(value => {
let strArray = value;
}).catch(error => {
console.log("getStringArrayValue promise error is " + error);
});
this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78);
} 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
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
| -------- | --------------------------- | ---- | --------------- |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**错误码:**
......@@ -593,51 +575,44 @@ getStringArrayValue(resource: Resource, callback: AsyncCallback&lt;Array&lt;stri
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001001 | If the module resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
**示例Stage:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
};
try {
this.context.resourceManager.getStringArrayValue(resource, (error, value) => {
this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
let str = value;
}
});
} 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
**模型约束**:此接口仅可在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;
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
};
try {
this.context.resourceManager.getStringArrayValue(resource).then(value => {
let strArray = value;
this.context.resourceManager.getStringValue($r('app.string.test').id).then(value => {
let str = value;
}).catch(error => {
console.log("getStringArray promise error is " + error);
console.log("getStringValue promise error is " + 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
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
**错误码:**
......@@ -690,37 +662,49 @@ getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
| -------- | ---------------------------------------- |
| 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.string.test').id
};
try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => {
this.context.resourceManager.getStringValue(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
let str = value;
}
});
} 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
**模型约束**:此接口仅可在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
| -------- | ---------------------------------------- |
| 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.string.test').id
};
try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error, value) => {
this.context.resourceManager.getStringValue(resource).then(value => {
let str = value;
}).catch(error => {
console.log("getStringValue promise error is " + error);
});
} catch (error) {
console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringByName<sup>9+</sup>
getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的字符串,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| 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.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
console.log("error is " + error);
} else {
let media = value;
let str = value;
}
});
} catch (error) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getStringByName 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): Promise&lt;string&gt;
用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组
用户获取指定资源名称对应的字符串,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |
| --------------------- | ---------- |
| Promise&lt;string&gt; | 资源名称对应的字符串 |
**错误码:**
......@@ -772,27 +800,28 @@ getMediaContent(resId: number): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 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.getMediaContent($r('app.media.test').id).then(value => {
let media = value;
this.context.resourceManager.getStringByName("test").then(value => {
let str = value;
}).catch(error => {
console.log("getMediaContent promise error is " + error);
console.log("getStringByName promise error is " + 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
......@@ -801,13 +830,12 @@ getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt;
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| 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;
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then(value => {
let media = value;
}).catch(error => {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
});
this.context.resourceManager.getStringArrayValueSync($r('app.strarray.test').id);
} 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
......@@ -844,9 +869,14 @@ getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;):
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| ----- | ------ | ---- | ----- |
| 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;):
| -------- | ---------------------------------------- |
| 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.media.test').id
id: $r('app.strarray.test').id
};
try {
this.context.resourceManager.getMediaContent(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
this.context.resourceManager.getStringArrayValueSync(resource);
} 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
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**错误码:**
......@@ -903,48 +925,42 @@ getMediaContent(resource: Resource, density: number, callback: AsyncCallback&lt;
| -------- | ---------------------------------------- |
| 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.media.test').id
};
try {
this.context.resourceManager.getMediaContent(resource, 120, (error, value) => {
this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => {
if (error != null) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
console.log("error is " + error);
} else {
let media = value;
let strArray = value;
}
});
} 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
**模型约束**:此接口仅可在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;
| -------- | ---------------------------------------- |
| 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.media.test').id
};
try {
this.context.resourceManager.getMediaContent(resource).then(value => {
let media = value;
this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then(value => {
let strArray = value;
}).catch(error => {
console.log("getMediaContent promise error is " + error);
console.log("getStringArrayValue promise error is " + 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
......@@ -986,15 +998,9 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt;
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------------- |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**错误码:**
......@@ -1004,39 +1010,49 @@ getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt;
| -------- | ---------------------------------------- |
| 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.media.test').id
id: $r('app.strarray.test').id
};
try {
this.context.resourceManager.getMediaContent(resource, 120).then(value => {
let media = value;
}).catch(error => {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
this.context.resourceManager.getStringArrayValue(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
} 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
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------------ |
| Promise&lt;Array&lt;string&gt;&gt; | resource对象对应的字符串数组 |
**错误码:**
......@@ -1046,37 +1062,40 @@ getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): voi
| -------- | ---------------------------------------- |
| 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.strarray.test').id
};
try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
this.context.resourceManager.getStringArrayValue(resource).then(value => {
let strArray = value;
}).catch(error => {
console.log("getStringArray promise error is " + 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
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
**错误码:**
......@@ -1084,43 +1103,44 @@ getMediaContentBase64(resId: number, density: number, callback: AsyncCallback&lt
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 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.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => {
this.context.resourceManager.getStringArrayByName("test", (error, value) => {
if (error != null) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
console.log("error is " + error);
} else {
let media = value;
let strArray = value;
}
});
} 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
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| 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;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 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.getMediaContentBase64($r('app.media.test').id).then(value => {
let media = value;
this.context.resourceManager.getStringArrayByName("test").then(value => {
let strArray = value;
}).catch(error => {
console.log("getMediaContentBase64 promise error is " + error);
console.log("getStringArrayByName promise error is " + 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
......@@ -1157,13 +1178,13 @@ getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt;
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| num | number | 是 | 数量值 |
**返回值:**
| 类型 | 说明 |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
| -------- | ----------- |
| string | 根据指定数量获取指定ID字符串表示的单复数字符串 |
**错误码:**
......@@ -1173,25 +1194,22 @@ getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt;
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then(value => {
let media = value;
}).catch(error => {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
});
this.context.resourceManager.getPluralStringValueSync($r('app.plural.test').id, 1);
} 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
......@@ -1200,9 +1218,15 @@ getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;)
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| ----- | ------ | ---- | ----- |
| 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;)
| -------- | ---------------------------------------- |
| 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.media.test').id
id: $r('app.plural.test').id
};
try {
this.context.resourceManager.getMediaContentBase64(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
this.context.resourceManager.getPluralStringValueSync(resource, 1);
} 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
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
| -------- | --------------------------- | ---- | ------------------------------- |
| resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 |
**错误码:**
......@@ -1259,48 +1276,84 @@ getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallba
| -------- | ---------------------------------------- |
| 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.media.test').id
};
try {
this.context.resourceManager.getMediaContentBase64(resource, 120, (error, value) => {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => {
if (error != null) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
console.log("error is " + error);
} else {
let media = value;
let str = value;
}
});
} 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
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
| Promise&lt;string&gt; | 根据提供的数量获取对应ID字符串表示的单复数字符串 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../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
try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then(value => {
let str = value;
}).catch(error => {
console.log("getPluralStringValue promise error is " + error);
});
} catch (error) {
console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getPluralStringValue<sup>9+</sup>
getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void
根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串 |
**错误码:**
......@@ -1310,30 +1363,33 @@ getMediaContentBase64(resource: Resource): Promise&lt;string&gt;
| -------- | ---------------------------------------- |
| 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.media.test').id
id: $r('app.plural.test').id
};
try {
this.context.resourceManager.getMediaContentBase64(resource).then(value => {
let media = value;
}).catch(error => {
console.log("getMediaContentBase64 promise error is " + error);
this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaContentBase64<sup>10+</sup>
### getPluralStringValue<sup>9+</sup>
getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt;
getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt;
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。
根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
......@@ -1344,13 +1400,13 @@ getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| num | number | 是 | 数量值 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
| --------------------- | ------------------------------ |
| Promise&lt;string&gt; | 根据提供的数量获取对应resource对象表示的单复数字符串 |
**错误码:**
......@@ -1360,159 +1416,222 @@ getMediaContentBase64(resource: Resource, density: number): Promise&lt;string&gt
| -------- | ---------------------------------------- |
| 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.media.test').id
id: $r('app.plural.test').id
};
try {
this.context.resourceManager.getMediaContentBase64(resource, 120).then(value => {
let media = value;
this.context.resourceManager.getPluralStringValue(resource, 1).then(value => {
let str = value;
}).catch(error => {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
console.log("getPluralStringValue promise error is " + 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}.`);
}
```
### getConfiguration
### getPluralStringByName<sup>9+</sup>
getConfiguration(callback: AsyncCallback&lt;Configuration&gt;): void
getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;string&gt;): void
用户获取设备的Configuration,使用callback形式返回Configuration对象
根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ------------------------- |
| callback | AsyncCallback&lt;[Configuration](#configuration)&gt; | 是 | 异步回调,用于返回设备的Configuration |
| -------- | --------------------------- | ---- | ----------------------------- |
| 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
try {
this.context.resourceManager.getConfiguration((error, value) => {
this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
if (error != null) {
console.error("getConfiguration callback error is " + error);
console.log("error is " + error);
} else {
let direction = value.direction;
let locale = value.locale;
let str = value;
}
});
} catch (error) {
console.error("getConfiguration callback error is " + error);
console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getPluralStringByName<sup>9+</sup>
### getConfiguration
getConfiguration(): Promise&lt;Configuration&gt;
getPluralStringByName(resName: string, num: number): Promise&lt;string&gt;
用户获取设备的Configuration,使用Promise形式返回Configuration对象
根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| num | number | 是 | 数量值 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ---------------- |
| Promise&lt;[Configuration](#configuration)&gt; | 设备的Configuration |
| --------------------- | ---------------------- |
| Promise&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.getConfiguration().then(value => {
let direction = value.direction;
let locale = value.locale;
this.context.resourceManager.getPluralStringByName("test", 1).then(value => {
let str = value;
}).catch(error => {
console.error("getConfiguration promise error is " + error);
console.log("getPluralStringByName promise error is " + error);
});
} catch (error) {
console.error("getConfiguration promise error is " + error);
console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaContentSync<sup>10+</sup>
### getDeviceCapability
getDeviceCapability(callback: AsyncCallback&lt;DeviceCapability&gt;): void
getMediaContentSync(resId: number, density?: number): Uint8Array
用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象
用户获取指定资源ID对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ---------------------------- |
| callback | AsyncCallback&lt;[DeviceCapability](#devicecapability)&gt; | 是 | 异步回调,用于返回设备的DeviceCapability |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| -------- | ----------- |
| Uint8Array | 资源ID对应的媒体文件内容 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
try {
this.context.resourceManager.getDeviceCapability((error, value) => {
if (error != null) {
console.error("getDeviceCapability callback error is " + error);
} else {
let screenDensity = value.screenDensity;
let deviceType = value.deviceType;
this.context.resourceManager.getMediaContentSync($r('app.media.test').id); // 默认屏幕密度
} catch (error) {
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("getDeviceCapability callback error is " + error);
console.error(`getMediaContentSync failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaContentSync<sup>10+</sup>
### getDeviceCapability
getDeviceCapability(): Promise&lt;DeviceCapability&gt;
getMediaContentSync(resource: Resource, density?: number): Uint8Array
用户获取设备的DeviceCapability,使用Promise形式返回DeviceCapability对象
用户获取指定resource对象对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ------------------- |
| Promise&lt;[DeviceCapability](#devicecapability)&gt; | 设备的DeviceCapability |
| --------------------- | ----------- |
| Uint8Array | resource对象对应的媒体文件内容 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getDeviceCapability().then(value => {
let screenDensity = value.screenDensity;
let deviceType = value.deviceType;
}).catch(error => {
console.error("getDeviceCapability promise error is " + error);
});
this.context.resourceManager.getMediaContentSync(resource); // 默认屏幕密度
} catch (error) {
console.error("getDeviceCapability promise error is " + error);
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(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void
getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串
用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------- |
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
......@@ -1522,44 +1641,37 @@ getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;stri
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => {
this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
let media = value;
}
});
} catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaContent<sup>10+</sup>
### getPluralStringValue<sup>9+</sup>
getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
getMediaContent(resId: number, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | 是 | 资源ID值 |
| num | number | 是 | 数量值 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | 根据提供的数量获取对应ID字符串表示的单复数字符串 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
......@@ -1569,38 +1681,41 @@ getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then(value => {
let str = value;
}).catch(error => {
console.log("getPluralStringValue promise error is " + error);
this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error, value) => {
if (error != null) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) {
console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getPluralStringValue<sup>9+</sup>
### getMediaContent<sup>9+</sup>
getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void
getMediaContent(resId: number): Promise&lt;Uint8Array&gt;
根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串
用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |
**错误码:**
......@@ -1610,50 +1725,40 @@ getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt
| -------- | ---------------------------------------- |
| 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.plural.test').id
};
try {
this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
this.context.resourceManager.getMediaContent($r('app.media.test').id).then(value => {
let media = value;
}).catch(error => {
console.log("getMediaContent promise error is " + error);
});
} catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getPluralStringValue<sup>9+</sup>
### getMediaContent<sup>10+</sup>
getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt;
getMediaContent(resId: number, density: number): Promise&lt;Uint8Array&gt;
根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| num | number | 是 | 数量值 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------------ |
| Promise&lt;string&gt; | 根据提供的数量获取对应resource对象表示的单复数字符串 |
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |
**错误码:**
......@@ -1663,41 +1768,36 @@ getPluralStringValue(resource: Resource, num: number): Promise&lt;string&gt;
| -------- | ---------------------------------------- |
| 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.plural.test').id
};
try {
this.context.resourceManager.getPluralStringValue(resource, 1).then(value => {
let str = value;
this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then(value => {
let media = value;
}).catch(error => {
console.log("getPluralStringValue promise error is " + error);
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaContent<sup>9+</sup>
### getRawFileContent<sup>9+</sup>
getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。
用户获取指定resource对象对应的媒体文件内容,使用callback形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的rawfile文件内容 |
| -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
......@@ -1705,42 +1805,46 @@ getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getRawFileContent("test.xml", (error, value) => {
this.context.resourceManager.getMediaContent(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let rawFile = value;
let media = value;
}
});
} 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>10+</sup>
getRawFileContent(path: string): Promise&lt;Uint8Array&gt;
getMediaContent(resource: Resource, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。
用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | rawfile文件内容 |
| -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
......@@ -1748,36 +1852,50 @@ getRawFileContent(path: string): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getRawFileContent("test.xml").then(value => {
let rawFile = value;
}).catch(error => {
console.log("getRawFileContent promise error is " + error);
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) {
console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaContent<sup>9+</sup>
### getRawFd<sup>9+</sup>
getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt;
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回
用户获取指定resource对象对应的媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | 是 | 异步回调,用于返回获取的rawfile文件的descriptor |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------------- |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
**错误码:**
......@@ -1785,44 +1903,49 @@ getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
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;
}
this.context.resourceManager.getMediaContent(resource).then(value => {
let media = value;
}).catch(error => {
console.log("getMediaContent promise error is " + 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>10+</sup>
getRawFd(path: string): Promise&lt;RawFileDescriptor&gt;
getMediaContent(resource: Resource, density: number): Promise&lt;Uint8Array&gt;
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回
用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ------------------- |
| Promise&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | rawfile文件descriptor |
| ------------------------- | ------------------- |
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |
**错误码:**
......@@ -1830,37 +1953,41 @@ getRawFd(path: string): Promise&lt;RawFileDescriptor&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getRawFd("test.xml").then(value => {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
this.context.resourceManager.getMediaContent(resource, 120).then(value => {
let media = value;
}).catch(error => {
console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
console.error(`promise getRawFd failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getRawFileList<sup>10+</sup>
### getMediaByName<sup>9+</sup>
getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): void;
getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下文件夹及文件列表,使用callback形式返回文件列表的字符串数组。
用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件夹路径 |
| callback | AsyncCallback&lt;Array\<string\>&gt; | 是 | 异步回调,用于返回获取rawfile文件目录下的文件列表 |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
......@@ -1868,42 +1995,39 @@ getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): vo
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:**
```ts
try { // 传入""表示获取rawfile根目录下的文件列表
this.context.resourceManager.getRawFileList("", (error, value) => {
try {
this.context.resourceManager.getMediaByName("test", (error, value) => {
if (error != null) {
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
console.log("error is " + error);
} else {
let rawFile = value;
let media = value;
}
});
} catch (error) {
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getRawFileList<sup>10+</sup>
### getMediaByName<sup>10+</sup>
getRawFileList(path: string): Promise&lt;Array\<string\>&gt;
getMediaByName(resName: string, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取resources/rawfile目录下文件夹及文件列表,使用Promise形式返回文件列表字符串数组。
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件夹路径 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Promise&lt;Array\<string\>&gt; | rawfile文件目录下的文件列表 |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
**错误码:**
......@@ -1911,35 +2035,43 @@ getRawFileList(path: string): Promise&lt;Array\<string\>&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:**
```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}.`);
try {
this.context.resourceManager.getMediaByName("test", 120, (error, value) => {
if (error != null) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) {
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### closeRawFd<sup>9+</sup>
### getMediaByName<sup>9+</sup>
closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void
getMediaByName(resName: string): Promise&lt;Uint8Array&gt;
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回
用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;void&gt; | 是 | 异步回调 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
**错误码:**
......@@ -1947,41 +2079,42 @@ closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | The resource not found by path. |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:**
```ts
try {
this.context.resourceManager.closeRawFd("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
}
this.context.resourceManager.getMediaByName("test").then(value => {
let media = value;
}).catch(error => {
console.log("getMediaByName promise error is " + error);
});
} catch (error) {
console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### closeRawFd<sup>9+</sup>
### getMediaByName<sup>10+</sup>
closeRawFd(path: string): Promise&lt;void&gt;
getMediaByName(resName: string, density: number): Promise&lt;Uint8Array&gt;
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回
用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ---- |
| Promise&lt;void&gt; | 无返回值 |
| ------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
**错误码:**
......@@ -1989,52 +2122,89 @@ closeRawFd(path: string): Promise&lt;void&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:**
```ts
try {
this.context.resourceManager.closeRawFd("test.xml").then(value => {
let result = value;
this.context.resourceManager.getMediaByName("test", 120).then(value => {
let media = value;
}).catch(error => {
console.log("closeRawFd promise error is " + error);
console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### release<sup>7+</sup>
### getMediaContentBase64Sync<sup>10+</sup>
release()
getMediaContentBase64Sync(resId: number, density?: number): string
用户释放创建的resourceManager
用户获取指定资源ID对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| -------- | ----------- |
| string | 资源ID对应的图片资源Base64编码 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
try {
this.context.resourceManager.release();
this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id); // 默认屏幕密度
} catch (error) {
console.error("release error is " + error);
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}.`);
}
```
### getStringByName<sup>9+</sup>
### getMediaContentBase64Sync<sup>10+</sup>
getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void
getMediaContentBase64Sync(resource: Resource, density?: number): string
用户获取指定资源名称对应的字符串,使用callback形式返回字符串。
用户获取指定resource对象对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的字符串 |
| ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| string | resource对象对应的图片资源Base64编码 |
**错误码:**
......@@ -2042,44 +2212,43 @@ getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getStringByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let string = value;
this.context.resourceManager.getMediaContentBase64Sync(resource); // 默认屏幕密度
} catch (error) {
console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`);
}
});
try {
this.context.resourceManager.getMediaContentBase64Sync(resource, 120); // 指定屏幕密度
} catch (error) {
console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringByName<sup>9+</sup>
### getMediaContentBase64<sup>9+</sup>
getStringByName(resName: string): Promise&lt;string&gt;
getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的字符串,使用Promise形式返回字符串。
用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
**参数:** Content
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------- |
| Promise&lt;string&gt; | 资源名称对应的字符串 |
| -------- | --------------------------- | ---- | ------------------------ |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:**
......@@ -2087,37 +2256,39 @@ getStringByName(resName: string): Promise&lt;string&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
try {
this.context.resourceManager.getStringByName("test").then(value => {
let string = value;
}).catch(error => {
console.log("getStringByName promise error is " + error);
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringArrayByName<sup>9+</sup>
### getMediaContentBase64<sup>10+</sup>
getStringArrayByName(resName: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
getMediaContentBase64(resId: number, density: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的字符串数组,使用callback形式返回字符串数组
用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是 | 异步回调,用于返回获取的字符串数组 |
| -------- | --------------------------- | ---- | ------------------------ |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:**
......@@ -2125,44 +2296,43 @@ getStringArrayByName(resName: string, callback: AsyncCallback&lt;Array&lt;string
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
try {
this.context.resourceManager.getStringArrayByName("test", (error, value) => {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => {
if (error != null) {
console.log("error is " + error);
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let strArray = value;
let media = value;
}
});
} catch (error) {
console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringArrayByName<sup>9+</sup>
### getMediaContentBase64<sup>9+</sup>
getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt;
getMediaContentBase64(resId: number): Promise&lt;string&gt;
用户获取指定资源名称对应的字符串数组,使用Promise形式返回字符串数组
用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------ |
| Promise&lt;Array&lt;string&gt;&gt; | 资源名称对应的字符串数组 |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
**错误码:**
......@@ -2170,37 +2340,42 @@ getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
try {
this.context.resourceManager.getStringArrayByName("test").then(value => {
let strArray = value;
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then(value => {
let media = value;
}).catch(error => {
console.log("getStringArrayByName promise error is " + error);
console.log("getMediaContentBase64 promise error is " + error);
});
} catch (error) {
console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaByName<sup>9+</sup>
### getMediaContentBase64<sup>10+</sup>
getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
getMediaContentBase64(resId: number, density: number): Promise&lt;string&gt;
用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组
用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
**错误码:**
......@@ -2208,39 +2383,38 @@ getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
try {
this.context.resourceManager.getMediaByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then(value => {
let media = value;
}
}).catch(error => {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaByName<sup>10+</sup>
### getMediaContentBase64<sup>9+</sup>
getMediaByName(resName: string, density: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组
用户获取指定resource对象对应的图片资源Base64编码,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的媒体文件内容 |
| -------- | --------------------------- | ---- | ------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:**
......@@ -2248,43 +2422,46 @@ getMediaByName(resName: string, density: number, callback: AsyncCallback&lt;Uint
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaByName("test", 120, (error, value) => {
this.context.resourceManager.getMediaContentBase64(resource, (error, value) => {
if (error != null) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
console.log("error is " + error);
} else {
let media = value;
}
});
} 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(resource: Resource, density: number, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
| -------- | --------------------------- | ---- | ------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,用于返回获取的图片资源Base64编码 |
**错误码:**
......@@ -2292,42 +2469,50 @@ getMediaByName(resName: string): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaByName("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;
}).catch(error => {
console.log("getMediaByName promise error is " + 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(resource: Resource): Promise&lt;string&gt;
用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组
用户获取指定resource对象对应的图片资源Base64编码,使用Promise形式返回字符串
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
**错误码:**
......@@ -2335,31 +2520,86 @@ getMediaByName(resName: string, density: number): Promise&lt;Uint8Array&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaByName("test", 120).then(value => {
this.context.resourceManager.getMediaContentBase64(resource).then(value => {
let media = value;
}).catch(error => {
console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
console.log("getMediaContentBase64 promise error is " + 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(resource: Resource, density: number): Promise&lt;string&gt;
用户获取指定资源名称对应的图片资源Base64编码,使用callback形式返回字符串。
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaContentBase64(resource, 120).then(value => {
let media = value;
}).catch(error => {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getMediaBase64ByName<sup>9+</sup>
getMediaBase64ByName(resName: string, callback: AsyncCallback&lt;string&gt;): void
用户获取指定资源名称对应的图片资源Base64编码,使用callback形式返回字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
......@@ -2515,21 +2755,26 @@ getMediaBase64ByName(resName: string, density: number): Promise&lt;string&gt;
}
```
### getPluralStringByName<sup>9+</sup>
### getDrawableDescriptor<sup>10+</sup>
getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;string&gt;): void
getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;
根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串
用户获取指定资源ID对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ----------------------------- |
| resName | string | 是 | 资源名称 |
| num | number | 是 | 数量值 |
| callback | AsyncCallback&lt;string&gt; | 是 | 异步回调,返回根据传入的数量值获取资源名称对应的字符串资源 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:**
......@@ -2537,46 +2782,45 @@ getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;s
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
try {
this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').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) {
console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getPluralStringByName<sup>9+</sup>
### getDrawableDescriptor<sup>10+</sup>
getPluralStringByName(resName: string, num: number): Promise&lt;string&gt;
getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串
用户获取指定resource对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| num | number | 是 | 数量值 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------------- |
| Promise&lt;string&gt; | 根据传入的数量值获取资源名称对应的字符串资源 |
| ------- | ----------------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:**
......@@ -2584,42 +2828,48 @@ getPluralStringByName(resName: string, num: number): Promise&lt;string&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.icon').id
};
try {
this.context.resourceManager.getPluralStringByName("test", 1).then(value => {
let str = value;
}).catch(error => {
console.log("getPluralStringByName promise error is " + error);
});
this.context.resourceManager.getDrawableDescriptor(resource);
} catch (error) {
console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getDrawableDescriptor(resource, 120);
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringSync<sup>9+</sup>
### getDrawableDescriptorByName<sup>10+</sup>
getStringSync(resId: number): string
getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor;
用户获取指定资源ID对应的字符串,使用同步方式返回字符串
用户获取指定资源名称对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| string | 资源ID值对应的字符串 |
| ------ | --------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
**错误码:**
......@@ -2627,24 +2877,28 @@ getStringSync(resId: number): string
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
**示例:**
```ts
try {
this.context.resourceManager.getStringSync($r('app.string.test').id);
this.context.resourceManager.getDrawableDescriptorByName('icon');
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getDrawableDescriptorByName('icon', 120);
} catch (error) {
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringSync<sup>10+</sup>
### getBoolean<sup>9+</sup>
getStringSync(resId: number, ...args: Array<string | number>): string
getBoolean(resId: number): boolean
用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串
使用同步方式,返回获取指定资源ID对应的布尔结果
**系统能力**:SystemCapability.Global.ResourceManager
......@@ -2653,39 +2907,36 @@ getStringSync(resId: number, ...args: Array<string | number>): string
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | 资源ID值对应的格式化字符串|
| ------- | ------------ |
| boolean | 资源ID值对应的布尔结果 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------------- |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 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
try {
this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78);
this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id);
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getBoolean<sup>9+</sup>
### getStringSync<sup>9+</sup>
getStringSync(resource: Resource): string
getBoolean(resource: Resource): boolean
用户获取指定resource对象对应的字符串,使用同步方式返回字符串
使用同步方式,返回获取指定resource对象对应的布尔结果
**系统能力**:SystemCapability.Global.ResourceManager
......@@ -2700,8 +2951,8 @@ getStringSync(resource: Resource): string
**返回值:**
| 类型 | 说明 |
| ------ | ---------------- |
| string | resource对象对应的字符串 |
| ------- | ----------------- |
| boolean | resource对象对应的布尔结果 |
**错误码:**
......@@ -2718,37 +2969,34 @@ getStringSync(resource: Resource): string
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
id: $r('app.boolean.boolean_test').id
};
try {
this.context.resourceManager.getStringSync(resource);
this.context.resourceManager.getBoolean(resource);
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringSync<sup>10+</sup>
### getBooleanByName<sup>9+</sup>
getStringSync(resource: Resource, ...args: Array<string | number>): string
getBooleanByName(resName: string): boolean
用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
使用同步方式,返回获取指定资源名称对应的布尔结果
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | resource对象对应的格式化字符串|
| ------- | ----------- |
| boolean | 资源名称对应的布尔结果 |
**错误码:**
......@@ -2756,44 +3004,38 @@ getStringSync(resource: Resource, ...args: Array<string | number>): string
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001003 | If the resName invalid. |
| 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
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
try {
this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78);
this.context.resourceManager.getBooleanByName("boolean_test");
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringByNameSync<sup>9+</sup>
### getNumber<sup>9+</sup>
getStringByNameSync(resName: string): string
getNumber(resId: number): number
用户获取指定资源名称对应的字符串,使用同步方式返回字符串
用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回资源对应的数值
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| string | 资源名称对应的字符串 |
| number | 资源ID值对应的数值。Integer对应的是原数值,float对应的是真实像素点值,具体参考示例代码 |
**错误码:**
......@@ -2801,39 +3043,46 @@ getStringByNameSync(resName: string): string
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getStringByNameSync("test");
this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer对应返回的是原数值
} catch (error) {
console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getNumber($r('app.float.float_test').id); // float对应返回的是真实像素点值
} catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getStringByNameSync<sup>10+</sup>
### getNumber<sup>9+</sup>
getStringByNameSync(resName: string, ...args: Array<string | number>): string
getNumber(resource: Resource): number
用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串
用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回资源对应的数值
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | 资源名称对应的格式化字符串|
| ------ | --------------- |
| number | resource对象对应的数值。Integer对应的是原数值,float对应的是真实像素点值, 具体参考示例代码 |
**错误码:**
......@@ -2841,39 +3090,43 @@ getStringByNameSync(resName: string, ...args: Array<string | number>): string
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001008 | If the resource obtained by resName formatting error. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.integer.integer_test').id
};
try {
this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78);
this.context.resourceManager.getNumber(resource);// integer对应返回的是原数值, float对应返回的是真实像素点值
} catch (error) {
console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getBoolean<sup>9+</sup>
### getNumberByName<sup>9+</sup>
getBoolean(resId: number): boolean
getNumberByName(resName: string): number
使用同步方式,返回获取指定资源ID对应的布尔结果
用户获取指定资源名称对应的integer数值或者float数值,使用同步方式资源对应的数值
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| ------- | ------------ |
| boolean | 资源ID值对应的布尔结果 |
| ------ | --------- |
| number | 资源名称对应的数值 |
**错误码:**
......@@ -2881,39 +3134,44 @@ getBoolean(resId: number): boolean
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 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.getBoolean($r('app.boolean.boolean_test').id);
this.context.resourceManager.getNumberByName("integer_test");
} catch (error) {
console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getNumberByName("float_test");
} catch (error) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getBoolean<sup>9+</sup>
getBoolean(resource: Resource): boolean
### getColorSync<sup>10+</sup>
使用同步方式,返回获取指定resource对象对应的布尔结果。
getColorSync(resId: number) : number;
**系统能力**:SystemCapability.Global.ResourceManager
用户获取指定资源ID对应的颜色值,使用同步方式返回其对应的颜色值。
**模型约束**:此接口仅可在Stage模型下使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------------- |
| boolean | resource对象对应的布尔结果 |
| ------ | ----------- |
| number | 资源ID值对应的颜色值(十进制) |
**错误码:**
......@@ -2927,37 +3185,34 @@ getBoolean(resource: Resource): boolean
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.boolean.boolean_test').id
};
try {
this.context.resourceManager.getBoolean(resource);
this.context.resourceManager.getColorSync($r('app.color.test').id);
} catch (error) {
console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getBooleanByName<sup>9+</sup>
### getColorSync<sup>10+</sup>
getBooleanByName(resName: string): boolean
getColorSync(resource: Resource): number
使用同步方式,返回获取指定资源名称对应的布尔结果
用户获取指定resource对象对应的颜色值,使用同步方式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------- |
| boolean | 资源名称对应的布尔结果 |
| ------ | ---------------- |
| number | resource对象对应的颜色值(十进制) |
**错误码:**
......@@ -2965,38 +3220,43 @@ getBooleanByName(resName: string): boolean
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 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.getBooleanByName("boolean_test");
this.context.resourceManager.getColorSync(resource);
} catch (error) {
console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getNumber<sup>9+</sup>
### getColorByNameSync<sup>10+</sup>
getNumber(resId: number): number
getColorByNameSync(resName: string) : number;
用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回资源对应的数值。
用户获取指定资源名称对应的颜色值,使用同步方式返回其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| number | 资源ID值对应的数值。Integer对应的是原数值,float对应的是真实像素点值,具体参考示例代码 |
| number | 资源名称对应的颜色值(十进制) |
**错误码:**
......@@ -3004,46 +3264,33 @@ getNumber(resId: number): number
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 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.getNumber($r('app.integer.integer_test').id); // integer对应返回的是原数值
} catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getNumber($r('app.float.float_test').id); // float对应返回的是真实像素点值
this.context.resourceManager.getColorByNameSync("test");
} catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`getColorByNameSync failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getNumber<sup>9+</sup>
getNumber(resource: Resource): number
### getColor<sup>10+</sup>
用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回资源对应的数值。
getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void;
**系统能力**:SystemCapability.Global.ResourceManager
用户获取指定资源ID对应的颜色值,使用callback形式返回其对应的颜色值。
**模型约束**:此接口仅可在Stage模型下使用。
**系统能力:** SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
**返回值:**
| 类型 | 说明 |
| ------ | --------------- |
| number | resource对象对应的数值。Integer对应的是原数值,float对应的是真实像素点值, 具体参考示例代码 |
| -------- | --------------------------- | ---- | --------------- |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) |
**错误码:**
......@@ -3051,43 +3298,44 @@ getNumber(resource: Resource): number
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001001 | If the module resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
**示例Stage:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.integer.integer_test').id
};
try {
this.context.resourceManager.getNumber(resource);// integer对应返回的是原数值, float对应返回的是真实像素点值
this.context.resourceManager.getColor($r('app.color.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getNumberByName<sup>9+</sup>
### getColor<sup>10+</sup>
getNumberByName(resName: string): number
getColor(resId: number): Promise&lt;number&gt;
用户获取指定资源名称对应的integer数值或者float数值,使用同步方式资源对应的数值。
用户获取指定资源ID对应的颜色值,使用Promise形式返回对应其对应的颜色值。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ------ | --------- |
| number | 资源名称对应的数值 |
| --------------------- | ----------- |
| Promise&lt;number&gt; | 资源ID值对应的颜色值(十进制) |
**错误码:**
......@@ -3095,45 +3343,39 @@ getNumberByName(resName: string): number
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**示例:**
```ts
try {
this.context.resourceManager.getNumberByName("integer_test");
} catch (error) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getNumberByName("float_test");
this.context.resourceManager.getColor($r('app.color.test').id).then(value => {
let str = value;
}).catch(error => {
console.log("getColor promise error is " + error);
});
} catch (error) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getDrawableDescriptor<sup>10+</sup>
### getColor<sup>10+</sup>
getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;
getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void;
用户获取指定资源ID对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
用户获取指定resource对象对应的颜色值,使用callback形式返回其对应的颜色值
**系统能力**:SystemCapability.Global.ResourceManager
**系统能力:** SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) |
**错误码:**
......@@ -3143,26 +3385,33 @@ getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor;
| -------- | ---------------------------------------- |
| 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.getDrawableDescriptor($r('app.media.icon').id);
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
this.context.resourceManager.getColor(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
try {
this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120);
});
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getDrawableDescriptor<sup>10+</sup>
### getColor<sup>10+</sup>
getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
getColor(resource: Resource): Promise&lt;number&gt;;
用户获取指定resource对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
用户获取指定resource对象对应的颜色值,使用Promise形式返回其对应的颜色值
**系统能力**:SystemCapability.Global.ResourceManager
......@@ -3173,13 +3422,12 @@ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
| --------------------- | ---------------- |
| Promise&lt;number&gt; | resource对象对应的颜色值(十进制) |
**错误码:**
......@@ -3189,31 +3437,71 @@ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor;
| -------- | ---------------------------------------- |
| 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.media.icon').id
id: $r('app.color.test').id
};
try {
this.context.resourceManager.getDrawableDescriptor(resource);
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
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.getDrawableDescriptor(resource, 120);
this.context.resourceManager.getColorByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let string = value;
}
});
} catch (error) {
console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getColorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getDrawableDescriptorByName<sup>10+</sup>
### getColorByName<sup>10+</sup>
getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor;
getColorByName(resName: string): Promise&lt;number&gt;
用户获取指定资源名称对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示
用户获取指定资源名称对应的颜色值,使用Promise形式返回其对应的颜色值
**系统能力**:SystemCapability.Global.ResourceManager
......@@ -3222,13 +3510,12 @@ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescript
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,默认为0 |
**返回值:**
| 类型 | 说明 |
| ------ | --------- |
| DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 |
| --------------------- | ---------- |
| Promise&lt;number&gt; | 资源名称对应的颜色值(十进制) |
**错误码:**
......@@ -3238,35 +3525,35 @@ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescript
| -------- | ---------------------------------------- |
| 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.getDrawableDescriptorByName('icon');
} catch (error) {
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
try {
this.context.resourceManager.getDrawableDescriptorByName('icon', 120);
this.context.resourceManager.getColorByName("test").then(value => {
let string = value;
}).catch(error => {
console.log("getColorByName promise error is " + error);
});
} catch (error) {
console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getColorByName failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColor<sup>10+</sup>
### getRawFileContent<sup>9+</sup>
getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void;
getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
用户获取指定资源ID对应的颜色值,使用callback形式返回其对应的颜色值
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组
**系统能力:** SystemCapability.Global.ResourceManager
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resId | number | 是 | 资源ID值 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是 | 异步回调,用于返回获取的rawfile文件内容 |
**错误码:**
......@@ -3274,44 +3561,42 @@ getColor(resId: number, callback: AsyncCallback&lt;number&gt;): void;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the module resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001005 | If the resource not found by path. |
**示例Stage:**
**示例:**
```ts
try {
this.context.resourceManager.getColor($r('app.color.test').id, (error, value) => {
this.context.resourceManager.getRawFileContent("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
let rawFile = value;
}
});
} catch (error) {
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColor<sup>10+</sup>
### getRawFileContent<sup>9+</sup>
getColor(resId: number): Promise&lt;number&gt;
getRawFileContent(path: string): Promise&lt;Uint8Array&gt;
用户获取指定资源ID对应的颜色值,使用Promise形式返回对应其对应的颜色值
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Promise&lt;number&gt; | 资源ID值对应的颜色值(十进制) |
| ------------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | rawfile文件内容 |
**错误码:**
......@@ -3319,39 +3604,35 @@ getColor(resId: number): Promise&lt;number&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
try {
this.context.resourceManager.getColor($r('app.color.test').id).then(value => {
let str = value;
this.context.resourceManager.getRawFileContent("test.xml").then(value => {
let rawFile = value;
}).catch(error => {
console.log("getColor promise error is " + error);
console.log("getRawFileContent promise error is " + error);
});
} catch (error) {
console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColor<sup>10+</sup>
getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void;
### getRawFileList<sup>10+</sup>
用户获取指定resource对象对应的颜色值,使用callback形式返回其对应的颜色值。
getRawFileList(path: string, callback: AsyncCallback&lt;Array\<string\>&gt;): void;
**系统能力:** SystemCapability.Global.ResourceManager
用户获取resources/rawfile目录下文件夹及文件列表,使用callback形式返回文件列表的字符串数组。
**模型约束**:此接口仅可在Stage模型下使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件夹路径 |
| callback | AsyncCallback&lt;Array\<string\>&gt; | 是 | 异步回调,用于返回获取rawfile文件目录下的文件列表 |
**错误码:**
......@@ -3359,51 +3640,42 @@ getColor(resource: Resource, callback: AsyncCallback&lt;number&gt;): void;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try {
this.context.resourceManager.getColor(resource, (error, value) => {
try { // 传入""表示获取rawfile根目录下的文件列表
this.context.resourceManager.getRawFileList("", (error, value) => {
if (error != null) {
console.log("error is " + error);
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let str = value;
let rawFile = value;
}
});
} catch (error) {
console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColor<sup>10+</sup>
### getRawFileList<sup>10+</sup>
getColor(resource: Resource): Promise&lt;number&gt;;
getRawFileList(path: string): Promise&lt;Array\<string\>&gt;
用户获取指定resource对象对应的颜色值,使用Promise形式返回其对应的颜色值
用户获取resources/rawfile目录下文件夹及文件列表,使用Promise形式返回文件列表字符串数组
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件夹路径 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------- |
| Promise&lt;number&gt; | resource对象对应的颜色值(十进制) |
| ------------------------- | ----------- |
| Promise&lt;Array\<string\>&gt; | rawfile文件目录下的文件列表 |
**错误码:**
......@@ -3411,42 +3683,35 @@ getColor(resource: Resource): Promise&lt;number&gt;;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001005 | If the resource not found by path. |
**示例:**
```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;
try { // 传入""表示获取rawfile根目录下的文件列表
this.context.resourceManager.getRawFileList("").then(value => {
let rawFile = value;
}).catch(error => {
console.log("getColor promise error is " + error);
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColorByName<sup>10+</sup>
### getRawFd<sup>9+</sup>
getColorByName(resName: string, callback: AsyncCallback&lt;number&gt;): void
getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
用户获取指定资源名称对应的颜色值,使用callback形式返回其对应的颜色值
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resName | string | 是 | 资源名称 |
| callback | AsyncCallback&lt;number&gt; | 是 | 异步回调,用于返回获取的颜色值(十进制) |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | 是 | 异步回调,用于返回获取的rawfile文件的descriptor |
**错误码:**
......@@ -3454,44 +3719,44 @@ getColorByName(resName: string, callback: AsyncCallback&lt;number&gt;): void
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
try {
this.context.resourceManager.getColorByName("test", (error, value) => {
this.context.resourceManager.getRawFd("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`);
} else {
let string = value;
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}
});
} catch (error) {
console.error(`callback getColorByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback getRawFd failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColorByName<sup>10+</sup>
### getRawFd<sup>9+</sup>
getColorByName(resName: string): Promise&lt;number&gt;
getRawFd(path: string): Promise&lt;RawFileDescriptor&gt;
用户获取指定资源名称对应的颜色值,使用Promise形式返回其对应的颜色值
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------- |
| Promise&lt;number&gt; | 资源名称对应的颜色值(十进制) |
| ---------------------------------------- | ------------------- |
| Promise&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | rawfile文件descriptor |
**错误码:**
......@@ -3499,42 +3764,37 @@ getColorByName(resName: string): Promise&lt;number&gt;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
try {
this.context.resourceManager.getColorByName("test").then(value => {
let string = value;
this.context.resourceManager.getRawFd("test.xml").then(value => {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}).catch(error => {
console.log("getColorByName promise error is " + error);
console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
console.error(`promise getColorByName failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise getRawFd failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColorSync<sup>10+</sup>
### closeRawFd<sup>9+</sup>
getColorSync(resId: number) : number;
closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void
用户获取指定资源ID对应的颜色值,使用同步方式返回其对应的颜色值
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| number | 资源ID值对应的颜色值(十进制) |
| -------- | ------------------------- | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
| callback | AsyncCallback&lt;void&gt; | 是 | 异步回调 |
**错误码:**
......@@ -3542,40 +3802,40 @@ getColorSync(resId: number) : number;
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001005 | The resource not found by path. |
**示例:**
```ts
try {
this.context.resourceManager.getColorSync($r('app.color.test').id);
this.context.resourceManager.closeRawFd("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
}
});
} catch (error) {
console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColorSync<sup>10+</sup>
### closeRawFd<sup>9+</sup>
getColorSync(resource: Resource): number
closeRawFd(path: string): Promise&lt;void&gt;
用户获取指定resource对象对应的颜色值,使用同步方式返回其对应的颜色值
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------------- |
| number | resource对象对应的颜色值(十进制) |
| ------------------- | ---- |
| Promise&lt;void&gt; | 无返回值 |
**错误码:**
......@@ -3583,60 +3843,151 @@ getColorSync(resource: Resource): number
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001005 | If the resource not found by path. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try {
this.context.resourceManager.getColorSync(resource);
this.context.resourceManager.closeRawFd("test.xml").then(value => {
let result = value;
}).catch(error => {
console.log("closeRawFd promise error is " + error);
});
} catch (error) {
console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`);
}
```
### getColorByNameSync<sup>10+</sup>
### getConfiguration
getColorByNameSync(resName: string) : number;
getConfiguration(callback: AsyncCallback&lt;Configuration&gt;): void
用户获取指定资源名称对应的颜色值,使用同步方式返回其对应的颜色值
用户获取设备的Configuration,使用callback形式返回Configuration对象
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| -------- | ---------------------------------------- | ---- | ------------------------- |
| callback | AsyncCallback&lt;[Configuration](#configuration)&gt; | 是 | 异步回调,用于返回设备的Configuration |
**示例:**
```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对象。
**系统能力**:SystemCapability.Global.ResourceManager
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| number | 资源名称对应的颜色值(十进制) |
| ---------------------------------------- | ---------------- |
| Promise&lt;[Configuration](#configuration)&gt; | 设备的Configuration |
**错误码:**
**示例:**
```ts
try {
this.context.resourceManager.getConfiguration().then(value => {
let direction = value.direction;
let locale = value.locale;
}).catch(error => {
console.error("getConfiguration promise error is " + error);
});
} catch (error) {
console.error("getConfiguration promise error is " + error);
}
```
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
### getDeviceCapability
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
getDeviceCapability(callback: AsyncCallback&lt;DeviceCapability&gt;): void
用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ---------------------------- |
| callback | AsyncCallback&lt;[DeviceCapability](#devicecapability)&gt; | 是 | 异步回调,用于返回设备的DeviceCapability |
**示例:**
```ts
try {
this.context.resourceManager.getColorByNameSync("test");
this.context.resourceManager.getDeviceCapability((error, value) => {
if (error != null) {
console.error("getDeviceCapability callback error is " + error);
} else {
let screenDensity = value.screenDensity;
let deviceType = value.deviceType;
}
});
} catch (error) {
console.error(`getColorByNameSync failed, error code: ${error.code}, message: ${error.message}.`);
console.error("getDeviceCapability callback error is " + error);
}
```
### getDeviceCapability
getDeviceCapability(): Promise&lt;DeviceCapability&gt;
用户获取设备的DeviceCapability,使用Promise形式返回DeviceCapability对象。
**系统能力**: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);
}
```
### release<sup>7+</sup>
release()
用户释放创建的resourceManager。
**系统能力**:SystemCapability.Global.ResourceManager
**示例:**
```ts
try {
this.context.resourceManager.release();
} catch (error) {
console.error("release error is " + error);
}
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册