1# @ohos.app.ability.dataUriUtils (DataUriUtils)
2
3The **DataUriUtils** module provides APIs to process URI objects. You can use the APIs to attach an ID to the end of a given URI and obtain, delete, or update the ID attached to the end of a given URI.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import { dataUriUtils } from '@kit.AbilityKit';
13```
14
15## dataUriUtils.getId
16
17getId(uri: string): number
18
19Obtains the ID attached to the end of a given URI.
20
21**System capability**: SystemCapability.Ability.AbilityRuntime.Core
22
23**Parameters**
24
25| Name | Type  | Mandatory | Description                       |
26| ---- | ------ | ---- | --------------------------- |
27| uri  | string | Yes  | Target URI object. |
28
29**Return value**
30
31| Type  | Description                    |
32| ------ | ------------------------ |
33| number | ID obtained. |
34
35**Error codes**
36
37For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
38
39| ID | Error Message |
40| ------- | -------- |
41| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
42
43**Example**
44
45```ts
46import { dataUriUtils } from '@kit.AbilityKit';
47
48try {
49  let id = dataUriUtils.getId('com.example.dataUriUtils/1221');
50  console.info(`get id: ${id}`);
51} catch(err) {
52  console.error(`get id err ,check the uri ${err}`);
53}
54```
55
56
57
58## dataUriUtils.attachId
59
60attachId(uri: string, id: number): string
61
62Attaches an ID to the end of a given URI.
63
64**System capability**: SystemCapability.Ability.AbilityRuntime.Core
65
66**Parameters**
67
68| Name | Type  | Mandatory | Description                       |
69| ---- | ------ | ---- | --------------------------- |
70| uri  | string | Yes  | Target URI object. |
71| id   | number | Yes  | ID to be attached.           |
72
73**Return value**
74
75| Type  | Description                 |
76| ------ | --------------------- |
77| string | URI object with the ID attached. |
78
79**Error codes**
80
81For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
82
83| ID | Error Message |
84| ------- | -------- |
85| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
86
87**Example**
88
89```ts
90import { dataUriUtils } from '@kit.AbilityKit';
91import { BusinessError } from '@kit.BasicServicesKit';
92
93let id = 1122;
94try {
95  let uri = dataUriUtils.attachId(
96    'com.example.dataUriUtils',
97    id,
98  );
99  console.info(`attachId the uri is: ${uri}`);
100} catch (err) {
101  console.error(`get id err, code: ${JSON.stringify((err as BusinessError).code)}, msg: ${JSON.stringify((err as BusinessError).message)}`);
102}
103```
104
105
106
107## dataUriUtils.deleteId
108
109deleteId(uri: string): string
110
111Deletes the ID from the end of a given URI.
112
113**System capability**: SystemCapability.Ability.AbilityRuntime.Core
114
115**Parameters**
116
117| Name | Type  | Mandatory | Description                       |
118| ---- | ------ | ---- | --------------------------- |
119| uri  | string | Yes  | URI object from which the ID is to be deleted. |
120
121**Return value**
122
123| Type  | Description               |
124| ------ | ------------------- |
125| string | URI object with the ID deleted. |
126
127**Error codes**
128
129For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
130
131| ID | Error Message |
132| ------- | -------- |
133| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
134
135**Example**
136
137```ts
138import { dataUriUtils } from '@kit.AbilityKit';
139import { BusinessError } from '@kit.BasicServicesKit';
140
141try {
142  let uri = dataUriUtils.deleteId('com.example.dataUriUtils/1221');
143  console.info(`delete id with the uri is: ${uri}`);
144} catch(err) {
145  console.error(`delete id err, code: ${JSON.stringify((err as BusinessError).code)}, msg: ${JSON.stringify((err as BusinessError).message)}`);
146}
147```
148
149
150
151## dataUriUtils.updateId
152
153updateId(uri: string, id: number): string
154
155Updates the ID in a given URI.
156
157**System capability**: SystemCapability.Ability.AbilityRuntime.Core
158
159**Parameters**
160
161| Name | Type  | Mandatory | Description               |
162| ---- | ------ | ---- | ------------------- |
163| uri  | string | Yes  | Target URI object. |
164| id   | number | Yes  | New ID.           |
165
166**Return value**
167
168| Type  | Description           |
169| ------ | --------------- |
170| string | URI object with the new ID. |
171
172**Error codes**
173
174For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
175
176| ID | Error Message |
177| ------- | -------- |
178| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
179
180**Example**
181
182```ts
183import { dataUriUtils } from '@kit.AbilityKit';
184import { BusinessError } from '@kit.BasicServicesKit';
185
186try {
187  let id = 1122;
188  let uri = dataUriUtils.updateId(
189    'com.example.dataUriUtils/1221',
190    id
191  );
192} catch (err) {
193  console.error(`update id err, code: ${JSON.stringify((err as BusinessError).code)}, msg: ${JSON.stringify((err as BusinessError).message)}`);
194}
195```
196