提交 b2f8f4f9 编写于 作者: S shawn_he

update doc

Signed-off-by: Nshawn_he <shawn.he@huawei.com>
上级 b420bfb3
# Resource Management
# Resource Manager
The resource management module provides APIs to obtain information about the current device configuration (including the language, region, screen direction, and MCC/MNC) and device capability (including the device type and resolution).
The Resource Manager module provides APIs to obtain information about application resources based on the current configuration, including the language, region, screen direction, MCC/MNC, as well as device capability and density.
> **NOTE**
>
......@@ -9,16 +9,23 @@ The resource management module provides APIs to obtain information about the cur
## Modules to Import
```
```js
import resourceManager from '@ohos.resourceManager';
```
## How to Use
## Instruction
Since API version 9, the stage model allows an application to obtain a **ResourceManager** object based on **context** and call its resource management APIs without first importing the required bundle. This method, however, is not applicable to the FA model.
Since API version 9, the stage model allows an application to obtain a **ResourceManager** object based on **context** and call its resource management APIs without first importing the required bundle. This approach, however, is not applicable to the FA model.
For details about how to reference **context** in the stage model, see [Context in the Stage Model](../../ability/context-userguide.md).
```
this.context.resourceManager;
```ts
import Ability from '@ohos.application.Ability';
class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
let context = this.context;
let resourceManager = context.resourceManager;
}
}
```
## resourceManager.getResourceManager
......@@ -27,17 +34,18 @@ getResourceManager(callback: AsyncCallback&lt;ResourceManager&gt;): void
Obtains the **ResourceManager** object of this application. This API uses an asynchronous callback to return the result.
This API can be used only in the FA model.
**Model restriction**: This API can be used only in the FA model.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ----------------------------- |
| callback | AsyncCallback&lt;[ResourceManager](#resourcemanager)&gt; | Yes | Callback used to return the result.|
**Example**
```
```js
resourceManager.getResourceManager((error, mgr) => {
if (error != null) {
console.log("error is " + error);
......@@ -60,18 +68,19 @@ getResourceManager(bundleName: string, callback: AsyncCallback&lt;ResourceManage
Obtains the **ResourceManager** object of an application based on the specified bundle name. This API uses an asynchronous callback to return the result.
This API can be used only in the FA model.
**Model restriction**: This API can be used only in the FA model.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ---------- | ---------------------------------------- | ---- | ----------------------------- |
| bundleName | string | Yes | Bundle name of the target application. |
| bundleName | string | Yes | Bundle name of the application. |
| callback | AsyncCallback&lt;[ResourceManager](#resourcemanager)&gt; | Yes | Callback used to return the result.|
**Example**
```
```js
resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => {
});
```
......@@ -83,7 +92,7 @@ getResourceManager(): Promise&lt;ResourceManager&gt;
Obtains the **ResourceManager** object of this application. This API uses a promise to return the result.
This API can be used only in the FA model.
**Model restriction**: This API can be used only in the FA model.
**System capability**: SystemCapability.Global.ResourceManager
......@@ -93,7 +102,7 @@ This API can be used only in the FA model.
| Promise&lt;[ResourceManager](#resourcemanager)&gt; | Promise used to return the result.|
**Example**
```
```js
resourceManager.getResourceManager().then(mgr => {
mgr.getString(0x1000000, (error, value) => {
if (error != null) {
......@@ -114,14 +123,15 @@ getResourceManager(bundleName: string): Promise&lt;ResourceManager&gt;
Obtains the **ResourceManager** object of an application based on the specified bundle name. This API uses a promise to return the result.
This API can be used only in the FA model.
**Model restriction**: This API can be used only in the FA model.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ---------- | ------ | ---- | ------------- |
| bundleName | string | Yes | Bundle name of the target application.|
| bundleName | string | Yes | Bundle name of the application.|
**Return value**
| Type | Description |
......@@ -129,11 +139,9 @@ This API can be used only in the FA model.
| Promise&lt;[ResourceManager](#resourcemanager)&gt; | Promise used to return the result.|
**Example**
```
```js
resourceManager.getResourceManager("com.example.myapplication").then(mgr => {
}).catch(error => {
});
```
......@@ -144,7 +152,7 @@ Enumerates the screen directions.
**System capability**: SystemCapability.Global.ResourceManager
| Name | Default Value | Description |
| Name | Value | Description |
| -------------------- | ---- | ---- |
| DIRECTION_VERTICAL | 0 | Portrait. |
| DIRECTION_HORIZONTAL | 1 | Landscape. |
......@@ -156,14 +164,14 @@ Enumerates the device types.
**System capability**: SystemCapability.Global.ResourceManager
| Name | Default Value | Description |
| Name | Value | Description |
| -------------------- | ---- | ---- |
| DEVICE_TYPE_PHONE | 0x00 | Phone. |
| DEVICE_TYPE_TABLET | 0x01 | Tablet. |
| DEVICE_TYPE_CAR | 0x02 | Head unit. |
| DEVICE_TYPE_PC | 0x03 | PC. |
| DEVICE_TYPE_TV | 0x04 | TV. |
| DEVICE_TYPE_WEARABLE | 0x06 | Wearable. |
| DEVICE_TYPE_PHONE | 0x00 | Phone |
| DEVICE_TYPE_TABLET | 0x01 | Tablet |
| DEVICE_TYPE_CAR | 0x02 | Head unit |
| DEVICE_TYPE_PC | 0x03 | PC |
| DEVICE_TYPE_TV | 0x04 | TV |
| DEVICE_TYPE_WEARABLE | 0x06 | Wearable |
## ScreenDensity
......@@ -172,7 +180,7 @@ Enumerates the screen density types.
**System capability**: SystemCapability.Global.ResourceManager
| Name | Default Value | Description |
| Name | Value | Description |
| -------------- | ---- | ---------- |
| SCREEN_SDPI | 120 | Screen density with small-scale dots per inch (SDPI). |
| SCREEN_MDPI | 160 | Screen density with medium-scale dots per inch (MDPI). |
......@@ -188,6 +196,7 @@ Defines the device configuration.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Readable | Writable | Description |
| --------- | ----------------------- | ---- | ---- | -------- |
......@@ -196,7 +205,7 @@ Defines the device configuration.
**Example**
```
```js
resourceManager.getResourceManager((error, mgr) => {
mgr.getConfiguration((error, value) => {
let direction = value.direction;
......@@ -211,6 +220,7 @@ Defines the device capability.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Readable | Writable | Description |
| ------------- | ------------------------------- | ---- | ---- | -------- |
......@@ -219,7 +229,7 @@ Defines the device capability.
**Example**
```
```js
resourceManager.getResourceManager((error, mgr) => {
mgr.getDeviceCapability((error, value) => {
let screenDensity = value.screenDensity;
......@@ -230,14 +240,17 @@ resourceManager.getResourceManager((error, mgr) => {
## RawFileDescriptor<sup>8+</sup>
Defines the descriptor of the raw file.<br>
Defines the descriptor of the raw file.
**System capability**: SystemCapability.Global.ResourceManager
| Name | Type | Description |
| ------ | ------ | ------------------ |
| fd | number | Descriptor of the raw file.|
| offset | number | Start offset of the raw file. |
| length | number | Length of the raw file. |
**Parameters**
| Name | Type | Readable | Writable | Description |
| ------ | ------ | ---- | ---- | ------------------ |
| fd | number | Yes | No| Descriptor of the raw file.|
| offset | number | Yes | No| Start offset of the raw file. |
| length | number | Yes | No| Length of the raw file. |
## Resource<sup>9+</sup>
......@@ -245,11 +258,13 @@ Defines the resource information of an application.
**System capability**: SystemCapability.Global.ResourceManager
| Name | Type | Description |
| ------ | ------ | ------------------ |
| bundleName | string | Bundle name of the application.|
| moduleName | string | Module name of the application. |
| id | number | Resource ID. |
**Parameters**
| Name | Type | Readable | Writable |Description |
| ---------- | ------ | ----- | ---- | ---------------|
| bundleName | string | Yes | No| Bundle name of the application.|
| moduleName | string | Yes | No| Module name of the application.|
| id | number | Yes | No| Resource ID. |
## ResourceManager
......@@ -258,48 +273,61 @@ Defines the capability of accessing application resources.
> **NOTE**
>
> - The methods involved in **ResourceManager** are applicable only to the TypeScript-based declarative development paradigm.
> - The APIs involved in **ResourceManager** are applicable only to the TypeScript-based declarative development paradigm.
>
> - Resource files are defined in the **resources** directory of the project. You can obtain the resource ID using **$r(resource address).id**, for example, **$r('app.string.test').id**.
### getStringValue<sup>9+</sup>
### getString
getString(resId: number, callback: AsyncCallback&lt;string&gt;): void
getStringValue(resId: number, callback: AsyncCallback&lt;string&gt;): void
Obtains the string corresponding to the specified resource ID. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | --------------- |
| resId | number | Yes | Resource ID. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getString($r('app.string.test').id, (error, value) => {
**Error codes**
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the module resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example stage**
```ts
try {
this.context.getStringValue($r('app.string.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
});
} catch (error) {
console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getString
### getStringValue<sup>9+</sup>
getString(resId: number): Promise&lt;string&gt;
getStringValue(resId: number): Promise&lt;string&gt;
Obtains the string corresponding to the specified resource ID. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
......@@ -309,117 +337,176 @@ Obtains the string corresponding to the specified resource ID. This API uses a p
| --------------------- | ----------- |
| Promise&lt;string&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getString($r('app.string.test').id).then(value => {
let str = value;
}).catch(error => {
console.log("getstring promise error is " + 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);
});
} catch (error) {
console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getString<sup>9+</sup>
### getStringValue<sup>9+</sup>
getString(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
Obtains the string corresponding to the specified resource object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | Yes | Resource object. |
| resource | [Resource](#resource9) | Yes | Resource object. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
this.context.resourceManager.getString(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
try {
this.context.resourceManager.getStringValue(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getString<sup>9+</sup>
getString(resource: Resource): Promise&lt;string&gt;
### getStringValue<sup>9+</sup>
getStringValue(resource: Resource): Promise&lt;string&gt;
Obtains the string corresponding to the specified resource object. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | Yes | Resource object.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Type | Description |
| --------------------- | ---------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
this.context.resourceManager.getString(resource).then(value => {
try {
this.context.resourceManager.getStringValue(resource).then(value => {
let str = value;
}).catch(error => {
console.log("getstring promise error is " + error);
});
}).catch(error => {
console.log("getStringValue promise error is " + error);
});
} catch (error) {
console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringArray
getStringArray(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
### getStringArrayValue<sup>9+</sup>
getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
Obtains the string array corresponding to the specified resource ID. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resId | number | Yes | Resource ID. |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getStringArray($r('app.strarray.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
});
```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;
}
});
} catch (error) {
console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringArray
### getStringArrayValue<sup>9+</sup>
getStringArray(resId: number): Promise&lt;Array&lt;string&gt;&gt;
getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt;
Obtains the string array corresponding to the specified resource ID. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
......@@ -429,116 +516,172 @@ Obtains the string array corresponding to the specified resource ID. This API us
| ---------------------------------- | ------------- |
| Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getStringArray($r('app.strarray.test').id).then(value => {
let strArray = value;
}).catch(error => {
console.log("getStringArray promise error is " + 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);
});
} catch (error) {
console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringArray<sup>9+</sup>
### getStringArrayValue<sup>9+</sup>
getStringArray(resource: Resource, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
getStringArrayValue(resource: Resource, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
Obtains the string array corresponding to the specified resource object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | Yes | Resource object. |
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resource | [Resource](#resource9) | Yes | Resource object. |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
};
this.context.resourceManager.getStringArray(resource, (error, value) => {
try {
this.context.resourceManager.getStringArrayValue(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
});
} catch (error) {
console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringArray<sup>9+</sup>
### getStringArrayValue<sup>9+</sup>
getStringArray(resource: Resource): Promise&lt;Array&lt;string&gt;&gt;
getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt;
Obtains the string array corresponding to the specified resource object. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | Yes | Resource object.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Type | Description |
| ---------------------------------- | ------------------ |
| Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
};
this.context.resourceManager.getStringArray(resource).then(value => {
try {
this.context.resourceManager.getStringArrayValue(resource).then(value => {
let strArray = value;
}).catch(error => {
console.log("getStringArray promise error is " + error);
});
}).catch(error => {
console.log("getStringArray promise error is " + error);
});
} catch (error) {
console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMedia
getMedia(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
### getMediaContent<sup>9+</sup>
getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
Obtains the content of the media file corresponding to the specified resource ID. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | Yes | Resource ID. |
| callback | AsyncCallback&lt;Uint8Array&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getMedia($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
});
```ts
try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMedia
### getMediaContent<sup>9+</sup>
getMedia(resId: number): Promise&lt;Uint8Array&gt;
getMediaContent(resId: number): Promise&lt;Uint8Array&gt;
Obtains the content of the media file corresponding to the specified resource ID. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
......@@ -548,116 +691,169 @@ Obtains the content of the media file corresponding to the specified resource ID
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getMedia($r('app.media.test').id).then(value => {
```ts
try {
this.context.resourceManager.getMediaContent($r('app.media.test').id).then(value => {
let media = value;
}).catch(error => {
console.log("getMedia promise error is " + error);
console.log("getMediaContent promise error is " + error);
});
});
} catch (error) {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMedia<sup>9+</sup>
### getMediaContent<sup>9+</sup>
getMedia(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): void
getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): void
Obtains the content of the media file corresponding to the specified resource object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | Yes | Resource object. |
| Name | Type | Mandatory | Description |
| -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9) | Yes | Resource object. |
| callback | AsyncCallback&lt;Uint8Array&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
this.context.resourceManager.getMedia(resource, (error, value) => {
if (error != null) {
try {
this.context.resourceManager.getMediaContent(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
} else {
let media = value;
}
});
}
});
} catch (error) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMedia<sup>9+</sup>
### getMediaContent<sup>9+</sup>
getMedia(resource: Resource): Promise&lt;Uint8Array&gt;
getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt;
Obtains the content of the media file corresponding to the specified resource object. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | Yes | Resource object.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Type | Description |
| ------------------------- | ------------------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
this.context.resourceManager.getMedia(resource).then(value => {
try {
this.context.resourceManager.getMediaContent(resource).then(value => {
let media = value;
}).catch(error => {
console.log("getMedia promise error is " + error);
});
}).catch(error => {
console.log("getMediaContent promise error is " + error);
});
} catch (error) {
console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMediaBase64
getMediaBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void
### getMediaContentBase64<sup>9+</sup>
getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void
Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------ |
| resId | number | Yes | Resource ID. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getMediaBase64($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
});
```ts
try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMediaBase64
### getMediaContentBase64<sup>9+</sup>
getMediaBase64(resId: number): Promise&lt;string&gt;
getMediaContentBase64(resId: number): Promise&lt;string&gt;
Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
......@@ -667,77 +863,116 @@ Obtains the Base64 code of the image corresponding to the specified resource ID.
| --------------------- | -------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getMediaBase64($r('app.media.test').id).then(value => {
let media = value;
}).catch(error => {
console.log("getMediaBase64 promise error is " + error);
});
});
```ts
try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then(value => {
let media = value;
}).catch(error => {
console.log("getMediaContentBase64 promise error is " + error);
});
} catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMediaBase64<sup>9+</sup>
### getMediaContentBase64<sup>9+</sup>
getMediaBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
Obtains the Base64 code of the image corresponding to the specified resource object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------ |
| resource | [Resource](#resource9) | Yes | Resource object. |
| resource | [Resource](#resource9) | Yes | Resource object. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
this.context.resourceManager.getMediaBase64(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
try {
this.context.resourceManager.getMediaContentBase64(resource, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMediaBase64<sup>9+</sup>
### getMediaContentBase64<sup>9+</sup>
getMediaBase64(resource: Resource): Promise&lt;string&gt;
getMediaContentBase64(resource: Resource): Promise&lt;string&gt;
Obtains the Base64 code of the image corresponding to the specified resource object. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | Yes | Resource object.|
**Return value**
| Type | Description |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
| Type | Description |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
this.context.resourceManager.getMediaBase64(resource).then(value => {
let media = value;
}).catch(error => {
console.log("getMediaBase64 promise error is " + error);
});
try {
this.context.resourceManager.getMediaContentBase64(resource).then(value => {
let media = value;
}).catch(error => {
console.log("getMediaContentBase64 promise error is " + error);
});
} catch (error) {
console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
}
```
......@@ -750,12 +985,13 @@ Obtains the device configuration. This API uses an asynchronous callback to retu
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ------------------------- |
| callback | AsyncCallback&lt;[Configuration](#configuration)&gt; | Yes | Callback used to return the result.|
**Example**
```
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getConfiguration((error, value) => {
if (error != null) {
......@@ -783,7 +1019,7 @@ Obtains the device configuration. This API uses a promise to return the result.
| Promise&lt;[Configuration](#configuration)&gt; | Promise used to return the result.|
**Example**
```
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getConfiguration().then(value => {
let direction = value.direction;
......@@ -804,12 +1040,13 @@ Obtains the device capability. This API uses an asynchronous callback to return
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------- |
| callback | AsyncCallback&lt;[DeviceCapability](#devicecapability)&gt; | Yes | Callback used to return the result.|
**Example**
```
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getDeviceCapability((error, value) => {
if (error != null) {
......@@ -837,7 +1074,7 @@ Obtains the device capability. This API uses a promise to return the result.
| Promise&lt;[DeviceCapability](#devicecapability)&gt; | Promise used to return the result.|
**Example**
```
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getDeviceCapability().then(value => {
let screenDensity = value.screenDensity;
......@@ -849,44 +1086,58 @@ Obtains the device capability. This API uses a promise to return the result.
```
### getPluralString
### getPluralStringValue<sup>9+</sup>
getPluralString(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void
getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void
Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------------- |
| resId | number | Yes | Resource ID. |
| num | number | Yes | Number. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getPluralString($r("app.plural.test").id, 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
});
```ts
try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getPluralString
### getPluralStringValue<sup>9+</sup>
getPluralString(resId: number, num: number): Promise&lt;string&gt;
getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
......@@ -897,46 +1148,74 @@ Obtains the singular-plural string corresponding to the specified resource ID ba
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getPluralString($r("app.plural.test").id, 1).then(value => {
let str = value;
}).catch(error => {
console.log("getPluralString promise error is " + error);
});
});
```
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
### getPluralString<sup>9+</sup>
**Error codes**
getPluralString(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```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(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getPluralStringValue<sup>9+</sup>
getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void
Obtains the singular-plural string corresponding to the specified resource object based on the specified number. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------------- |
| resource | [Resource](#resource9) | Yes | Resource object. |
| num | number | Yes | Number. |
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------------------ |
| resource | [Resource](#resource9) | Yes | Resource object. |
| num | number | Yes | Number. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.plural.test').id
};
this.context.resourceManager.getPluralString(resource, 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
try {
this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getPluralString<sup>9+</sup>
......@@ -948,66 +1227,95 @@ Obtains the singular-plural string corresponding to the specified resource objec
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | Yes | Resource object.|
| num | number | Yes | Number. |
| num | number | Yes | Number. |
**Return value**
| Type | Description |
| --------------------- | ------------------------- |
| Type | Description |
| --------------------- | ------------------------------ |
| Promise&lt;string&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.plural.test').id
};
this.context.resourceManager.getPluralString(resource, 1).then(value => {
let str = value;
}).catch(error => {
console.log("getPluralString promise error is " + error);
});
try {
this.context.resourceManager.getPluralString(resource, 1).then(value => {
let str = value;
}).catch(error => {
console.log("getPluralString promise error is " + error);
});
} catch (error) {
console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getRawFile<sup>8+</sup>
getRawFile(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
### getRawFileContent<sup>9+</sup>
getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
Obtains the content of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | Yes | Path of the raw file. |
| callback | AsyncCallback&lt;Uint8Array&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFile("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let rawFile = value;
}
});
});
```ts
try {
this.context.resourceManager.getRawFileContent("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let rawFile = value;
}
});
} catch (error) {
console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getRawFile<sup>8+</sup>
### getRawFileContent<sup>9+</sup>
getRawFile(path: string): Promise&lt;Uint8Array&gt;
getRawFileContent(path: string): Promise&lt;Uint8Array&gt;
Obtains the content of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------- |
| path | string | Yes | Path of the raw file.|
......@@ -1017,55 +1325,77 @@ Obtains the content of the raw file in the **resources/rawfile** directory. This
| ------------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFile("test.xml").then(value => {
let rawFile = value;
}).catch(error => {
console.log("getRawFile promise error is " + error);
});
});
```ts
try {
this.context.resourceManager.getRawFileContent("test.xml").then(value => {
let rawFile = value;
}).catch(error => {
console.log("getRawFileContent promise error is " + error);
});
} catch (error) {
console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getRawFileDescriptor<sup>8+</sup>
getRawFileDescriptor(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
### getRawFd<sup>9+</sup>
getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path | string | Yes | Path of the raw file. |
| callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFileDescriptor("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}
});
```ts
try {
this.context.resourceManager.getRawFd("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}
}).catch(error => {
console.log("getRawFd callback error is " + error);
});
```
### getRawFileDescriptor<sup>8+</sup>
### getRawFd<sup>9+</sup>
getRawFileDescriptor(path: string): Promise&lt;RawFileDescriptor&gt;
getRawFd(path: string): Promise&lt;RawFileDescriptor&gt;
Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------- |
| path | string | Yes | Path of the raw file.|
......@@ -1075,17 +1405,27 @@ Obtains the descriptor of the raw file in the **resources/rawfile** directory. T
| ---------------------------------------- | ------------------- |
| Promise&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**Example**
```
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFileDescriptor("test.xml").then(value => {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}).catch(error => {
console.log("getRawFileDescriptor promise error is " + error);
});
});
```ts
try {
this.context.resourceManager.getRawFd("test.xml").then(value => {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}).catch(error => {
console.log("getRawFd promise error is " + error);
});
} catch (error) {
console.log("getRawFd promise error is " + error);
};
```
### closeRawFileDescriptor<sup>8+</sup>
......@@ -1097,13 +1437,14 @@ Closes the descriptor of the raw file in the **resources/rawfile** directory. Th
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------- | ---- | ----------- |
| path | string | Yes | Path of the raw file.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result. |
**Example**
```
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.closeRawFileDescriptor("test.xml", (error, value) => {
if (error != null) {
......@@ -1122,6 +1463,7 @@ Closes the descriptor of the raw file in the **resources/rawfile** directory. Th
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------- |
| path | string | Yes | Path of the raw file.|
......@@ -1132,7 +1474,7 @@ Closes the descriptor of the raw file in the **resources/rawfile** directory. Th
| Promise&lt;void&gt; | Promise that returns no value.|
**Example**
```
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.closeRawFileDescriptor("test.xml").then(value => {
let result = value;
......@@ -1142,6 +1484,84 @@ Closes the descriptor of the raw file in the **resources/rawfile** directory. Th
});
```
### closeRawFd<sup>9+</sup>
closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void
Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------- | ---- | ----------- |
| path | string | Yes | Path of the raw file.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result. |
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001005 | The resource not found by path. |
**Example**
```ts
try {
this.context.resourceManager.closeRawFd("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
}
});
} catch (error) {
console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### closeRawFd<sup>8+</sup>
closeRawFd(path: string): Promise&lt;void&gt;
Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------- |
| path | string | Yes | Path of the raw file.|
**Return value**
| Type | Description |
| ------------------- | ---- |
| Promise&lt;void&gt; | Promise that returns no value.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001005 | If the resource not found by path. |
**Example**
```ts
try {
this.context.resourceManager.closeRawFd("test.xml").then(value => {
let result = value;
}).catch(error => {
console.log("closeRawFd promise error is " + error);
});
} catch (error) {
console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### release<sup>7+</sup>
release()
......@@ -1151,7 +1571,7 @@ Releases a created **resourceManager** object.
**System capability**: SystemCapability.Global.ResourceManager
**Example**
```
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.release();
});
......@@ -1166,20 +1586,36 @@ Obtains the string corresponding to the specified resource name. This API uses a
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | --------------- |
| resName | string | Yes | Resource name. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
| resName | string | Yes | Resource name. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getStringByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let string = value;
}
});
```ts
try {
this.context.resourceManager.getStringByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let string = value;
}
});
} catch (error) {
console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringByName<sup>9+</sup>
......@@ -1191,22 +1627,37 @@ Obtains the string corresponding to the specified resource name. This API uses a
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ---- |
| resName | string | Yes | Resource name.|
| resName | string | Yes | Resource name.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Promise&lt;string&gt; | Promise used to return the result.|
| Type | Description |
| --------------------- | ---------- |
| Promise&lt;string&gt; | String corresponding to the resource name.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getStringByName("test").then(value => {
let string = value;
}).catch(error => {
console.log("getStringByName promise error is " + error);
});
```ts
try {
this.context.resourceManager.getStringByName("test").then(value => {
let string = value;
}).catch(error => {
console.log("getStringByName promise error is " + error);
});
} catch (error) {
console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringArrayByName<sup>9+</sup>
......@@ -1218,20 +1669,35 @@ Obtains the string array corresponding to the specified resource name. This API
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resName | string | Yes | Resource name. |
| resName | string | Yes | Resource name. |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getStringArrayByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
```ts
try {
this.context.resourceManager.getStringArrayByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
} catch (error) {
console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringArrayByName<sup>9+</sup>
......@@ -1243,22 +1709,37 @@ Obtains the string array corresponding to the specified resource name. This API
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ----- |
| resName | string | Yes | Resource name.|
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ---- |
| resName | string | Yes | Resource name.|
**Return value**
| Type | Description |
| ---------------------------------- | ------------- |
| Type | Description |
| ---------------------------------- | ------------ |
| Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getStringArrayByName("test").then(value => {
let strArray = value;
}).catch(error => {
console.log("getStringArrayByName promise error is " + error);
});
```ts
try {
this.context.resourceManager.getStringArrayByName("test").then(value => {
let strArray = value;
}).catch(error => {
console.log("getStringArrayByName promise error is " + error);
});
} catch (error) {
console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMediaByName<sup>9+</sup>
......@@ -1270,20 +1751,35 @@ Obtains the content of the media file corresponding to the specified resource na
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | Yes | Resource name. |
| resName | string | Yes | Resource name. |
| callback | AsyncCallback&lt;Uint8Array&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getMediaByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
```ts
try {
this.context.resourceManager.getMediaByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMediaByName<sup>9+</sup>
......@@ -1295,22 +1791,37 @@ Obtains the content of the media file corresponding to the specified resource na
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ---- |
| resName | string | Yes | Resource name.|
**Return value**
| Type | Description |
| ------------------------- | -------------- |
| Type | Description |
| ------------------------- | ------------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getMediaByName("test").then(value => {
let media = value;
}).catch(error => {
console.log("getMediaByName promise error is " + error);
});
```ts
try {
this.context.resourceManager.getMediaByName("test").then(value => {
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}.`)
}
```
### getMediaBase64ByName<sup>9+</sup>
......@@ -1322,20 +1833,35 @@ Obtains the Base64 code of the image corresponding to the specified resource nam
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------ |
| resName | string | Yes | Resource name. |
| resName | string | Yes | Resource name. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getMediaBase64ByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
```ts
try {
this.context.resourceManager.getMediaBase64ByName("test", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getMediaBase64ByName<sup>9+</sup>
......@@ -1347,22 +1873,37 @@ Obtains the Base64 code of the image corresponding to the specified resource nam
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ----- |
| resName | string | Yes | Resource name.|
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ---- |
| resName | string | Yes | Resource name.|
**Return value**
| Type | Description |
| --------------------- | -------------------- |
| Type | Description |
| --------------------- | ------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getMediaBase64ByName("test").then(value => {
let media = value;
}).catch(error => {
console.log("getMediaBase64ByName promise error is " + error);
});
```ts
try {
this.context.resourceManager.getMediaBase64ByName("test").then(value => {
let media = value;
}).catch(error => {
console.log("getMediaBase64ByName promise error is " + error);
});
} catch (error) {
console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getPluralStringByName<sup>9+</sup>
......@@ -1374,21 +1915,37 @@ Obtains the plural string corresponding to the specified resource name based on
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------------- |
| resName | string | Yes | Resource name. |
| num | number | Yes | Number. |
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ----------------------------- |
| resName | string | Yes | Resource name. |
| num | number | Yes | Number. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
```ts
try {
this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getPluralStringByName<sup>9+</sup>
......@@ -1400,23 +1957,38 @@ Obtains the plural string corresponding to the specified resource name based on
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ---- |
| resName | string | Yes | Resource name.|
| num | number | Yes | Number. |
| num | number | Yes | Number. |
**Return value**
| Type | Description |
| --------------------- | ------------------------- |
| Type | Description |
| --------------------- | ---------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getPluralStringByName("test", 1).then(value => {
```ts
try {
this.context.resourceManager.getPluralStringByName("test", 1).then(value => {
let str = value;
}).catch(error => {
}).catch(error => {
console.log("getPluralStringByName promise error is " + error);
});
});
} catch (error) {
console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringSync<sup>9+</sup>
......@@ -1428,18 +2000,33 @@ Obtains the string corresponding to the specified resource ID. This API returns
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| string | String corresponding to the specified resource ID.|
| Type | Description |
| ------ | ----------- |
| string | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getStringSync($r('app.string.test').id);
```ts
try {
this.context.resourceManager.getStringSync($r('app.string.test').id);
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringSync<sup>9+</sup>
......@@ -1451,23 +2038,38 @@ Obtains the string corresponding to the specified resource object. This API retu
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | Yes | Resource object.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| string | String corresponding to the resource object.|
| Type | Description |
| ------ | ---------------- |
| string | Promise used to return the result.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
this.context.resourceManager.getStringSync(resource);
try {
this.context.resourceManager.getStringSync(resource);
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringByNameSync<sup>9+</sup>
......@@ -1479,18 +2081,33 @@ Obtains the string corresponding to the specified resource name. This API return
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ---- |
| resName | string | Yes | Resource name.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| string | String corresponding to the resource name.|
| Type | Description |
| ------ | ---------- |
| string | String corresponding to the specified resource name.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getStringByNameSync("test");
```ts
try {
this.context.resourceManager.getStringByNameSync("test");
} catch (error) {
console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getBoolean<sup>9+</sup>
......@@ -1502,18 +2119,33 @@ Obtains the Boolean result corresponding to the specified resource ID. This API
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Type | Description |
| ------- | ------------ |
| boolean | Boolean result corresponding to the specified resource ID.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id);
```ts
try {
this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id);
} catch (error) {
console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getBoolean<sup>9+</sup>
......@@ -1524,23 +2156,38 @@ Obtains the Boolean result corresponding to the specified resource object. This
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | Yes | Resource object.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Type | Description |
| ------- | ----------------- |
| boolean | Boolean result corresponding to the specified resource object.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.boolean.boolean_test').id
};
this.context.resourceManager.getBoolean(resource);
try {
this.context.resourceManager.getBoolean(resource);
} catch (error) {
console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getBooleanByName<sup>9+</sup>
......@@ -1552,18 +2199,33 @@ Obtains the Boolean result corresponding to the specified resource name. This AP
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ----- |
| resName | string | Yes | Resource name.|
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ---- |
| resName | string | Yes | Resource name.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Type | Description |
| ------- | ----------- |
| boolean | Boolean result corresponding to the specified resource name.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getBooleanByName("boolean_test");
```ts
try {
this.context.resourceManager.getBooleanByName("boolean_test");
} catch (error) {
console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getNumber<sup>9+</sup>
......@@ -1575,19 +2237,39 @@ Obtains the integer or float value corresponding to the specified resource ID. T
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Type | Description |
| ------ | ---------- |
| number | Integer or float value corresponding to the specified resource ID.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
this.context.resourceManager.getNumber($r('app.integer.integer_test').id);
this.context.resourceManager.getNumber($r('app.float.float_test').id);
```ts
try {
this.context.resourceManager.getNumber($r('app.integer.integer_test').id);
} catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
}
try {
this.context.resourceManager.getNumber($r('app.float.float_test').id);
} catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getNumber<sup>9+</sup>
......@@ -1599,23 +2281,38 @@ Obtains the integer or float value corresponding to the specified resource objec
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| Name | Type | Mandatory | Description |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | Yes | Resource object.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Type | Description |
| ------ | --------------- |
| number | Integer or float value corresponding to the specified resource object.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
**Example**
```
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.integer.integer_test').id
};
this.context.resourceManager.getNumber(resource);
try {
this.context.resourceManager.getNumber(resource);
} catch (error) {
console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getNumberByName<sup>9+</sup>
......@@ -1627,17 +2324,490 @@ Obtains the integer or float value corresponding to the specified resource name.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ------- | ------ | ---- | ---- |
| resName | string | Yes | Resource name.|
**Return value**
| Type | Description |
| ------ | --------- |
| number | Integer or float value corresponding to the specified resource name.|
For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md).
**Error codes**
| ID| Error Message|
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
**Example**
```ts
try {
this.context.resourceManager.getNumberByName("integer_test");
} catch (error) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`)
}
try {
this.context.resourceManager.getNumberByName("float_test");
} catch (error) {
console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getString<sup>(deprecated)</sup>
getString(resId: number, callback: AsyncCallback&lt;string&gt;): void
Obtains the string corresponding to the specified resource ID. This API uses an asynchronous callback to return the result.
This API is deprecated since API version 9. You are advised to use [getStringValue](#getstringvalue9) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | --------------- |
| resId | number | Yes | Resource ID. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getString($r('app.string.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
});
```
### getString<sup>(deprecated)</sup>
getString(resId: number): Promise&lt;string&gt;
Obtains the string corresponding to the specified resource ID. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use [getStringValue](#getstringvalue9-1) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resName | string | Yes | Resource name.|
| resId | number | Yes | Resource ID.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| number | Integer or float value corresponding to the specified resource name.|
| Promise&lt;string&gt; | Promise used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getString($r('app.string.test').id).then(value => {
let str = value;
}).catch(error => {
console.log("getstring promise error is " + error);
});
});
```
### getStringArray<sup>(deprecated)</sup>
getStringArray(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
Obtains the string array corresponding to the specified resource ID. This API uses an asynchronous callback to return the result.
This API is deprecated since API version 9. You are advised to use [getStringArrayValue](#getstringarrayvalue9) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resId | number | Yes | Resource ID. |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes | Callback used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getStringArray($r('app.strarray.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let strArray = value;
}
});
});
```
this.context.resourceManager.getNumberByName("integer_test");
this.context.resourceManager.getNumberByName("float_test");
### getStringArray<sup>(deprecated)</sup>
getStringArray(resId: number): Promise&lt;Array&lt;string&gt;&gt;
Obtains the string array corresponding to the specified resource ID. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use [getStringArrayValue](#getstringarrayvalue9-1) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
**Return value**
| Type | Description |
| ---------------------------------- | ------------- |
| Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getStringArray($r('app.strarray.test').id).then(value => {
let strArray = value;
}).catch(error => {
console.log("getStringArray promise error is " + error);
});
});
```
### getMedia<sup>(deprecated)</sup>
getMedia(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
Obtains the content of the media file corresponding to the specified resource ID. This API uses an asynchronous callback to return the result.
This API is deprecated since API version 9. You are advised to use [getMediaContent](#getmediacontent) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | Yes | Resource ID. |
| callback | AsyncCallback&lt;Uint8Array&gt; | Yes | Callback used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getMedia($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
});
```
### getMedia<sup>(deprecated)</sup>
getMedia(resId: number): Promise&lt;Uint8Array&gt;
Obtains the content of the media file corresponding to the specified resource ID. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use [getMediaContent](#getmediacontent-1) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
**Return value**
| Type | Description |
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getMedia($r('app.media.test').id).then(value => {
let media = value;
}).catch(error => {
console.log("getMedia promise error is " + error);
});
});
```
### getMediaBase64<sup>(deprecated)</sup>
getMediaBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void
Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses an asynchronous callback to return the result.
This API is deprecated since API version 9. You are advised to use [getMediaContentBase64](#getmediacontentbase64) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------ |
| resId | number | Yes | Resource ID. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getMediaBase64($r('app.media.test').id, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let media = value;
}
});
});
```
### getMediaBase64<sup>(deprecated)</sup>
getMediaBase64(resId: number): Promise&lt;string&gt;
Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use [getMediaContentBase64](#getmediacontentbase64-1) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
**Return value**
| Type | Description |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getMediaBase64($r('app.media.test').id).then(value => {
let media = value;
}).catch(error => {
console.log("getMediaBase64 promise error is " + error);
});
});
```
### getPluralString<sup>(deprecated)</sup>
getPluralString(resId: number, num: number): Promise&lt;string&gt;
Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use [getPluralStringValue](#getpluralstringvalue) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| resId | number | Yes | Resource ID.|
| num | number | Yes | Number. |
**Return value**
| Type | Description |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getPluralString($r("app.plural.test").id, 1).then(value => {
let str = value;
}).catch(error => {
console.log("getPluralString promise error is " + error);
});
});
```
### getPluralString<sup>(deprecated)</sup>
getPluralString(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void
Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses an asynchronous callback to return the result.
This API is deprecated since API version 9. You are advised to use [getPluralStringValue](#getpluralstringvalue-1) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------------- | ---- | ------------------------------- |
| resId | number | Yes | Resource ID. |
| num | number | Yes | Number. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getPluralString($r("app.plural.test").id, 1, (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let str = value;
}
});
});
```
### getRawFile<sup>(deprecated)</sup>
getRawFile(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
Obtains the content of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result.
This API is deprecated since API version 9. You are advised to use [getRawFileContent](#getrawfilecontent9) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | Yes | Path of the raw file. |
| callback | AsyncCallback&lt;Uint8Array&gt; | Yes | Callback used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFile("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let rawFile = value;
}
});
});
```
### getRawFile<sup>(deprecated)</sup>
getRawFile(path: string): Promise&lt;Uint8Array&gt;
Obtains the content of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use [getRawFileContent](#getrawfilecontent9-1) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------- |
| path | string | Yes | Path of the raw file.|
**Return value**
| Type | Description |
| ------------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFile("test.xml").then(value => {
let rawFile = value;
}).catch(error => {
console.log("getRawFile promise error is " + error);
});
});
```
### getRawFileDescriptor<sup>(deprecated)</sup>
getRawFileDescriptor(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result.
This API is deprecated since API version 9. You are advised to use [getRawFd](#getrawfd9) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path | string | Yes | Path of the raw file. |
| callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | Yes | Callback used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFileDescriptor("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}
});
});
```
### getRawFileDescriptor<sup>(deprecated)</sup>
getRawFileDescriptor(path: string): Promise&lt;RawFileDescriptor&gt;
Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use [getRawFd](#getrawfd9-1) instead.
**System capability**: SystemCapability.Global.ResourceManager
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------- |
| path | string | Yes | Path of the raw file.|
**Return value**
| Type | Description |
| ---------------------------------------- | ------------------- |
| Promise&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | Promise used to return the result.|
**Example**
```ts
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFileDescriptor("test.xml").then(value => {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
}).catch(error => {
console.log("getRawFileDescriptor promise error is " + error);
});
});
```
# Resource Manager Error Codes
## 9001001 Invalid Resource ID
**Error Message**
The resId invalid.
**Description**
This error code is reported if the specified resource ID meets the type requirement but the resource ID does not exist.
**Possible Causes**
The specified resource ID does not exist.
**Solution**
Check whether the specified resource ID exists.
## 9001002 Matching Resource Not Found Based on the Specified Resource ID
**Error Message**
The resource not found by resId.
**Description**
This error code is reported if the specified resource ID meets the type requirement but the no resource is found based on the resource ID.
**Possible Causes**
1. The specified resource ID is incorrect.
2. Resource parsing is incorrect.
**Solution**
Check whether the specified resource ID is correct.
## 9001003 Invalid Resource Name
**Error Message**
The resName invalid.
**Description**
This error code is reported if the specified resource name meets the type requirement but the resource name does not exist.
**Possible Causes**
The specified resource name does not exist.
**Solution**
Check whether the specified resource name is correct.
## 9001004 Matching Resource Not Found Based on the Specified Resource Name
**Error Message**
The resource not found by resName.
**Description**
This error code is reported if the specified resource ID meets the type requirement but the no resource is found based on the resource ID.
**Possible Causes**
1. The specified resource name is incorrect.
2. Resource parsing is incorrect.
**Solution**
Check whether the specified resource name is correct.
## 9001005 Invalid Relative Path
**Error Message**
The resource not found by path.
**Description**
This error code is reported if no resource is found based on the specified relative path.
**Possible Causes**
The specified relative path is incorrect.
**Solution**
Check whether the specified relative path is correct.
## 9001006 Cyclic Reference
**Error Message**
the resource re-ref too much.
**Description**
This error code is reported if resources are referenced cyclically.
**Possible Causes**
Resources are referenced cyclically.
**Solution**
Check the reference of resource $, and remove the cyclic reference, if any.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册