1# @ohos.telephony.vcard (VCard模块)(系统接口)
2
3VCard是电子名片的文件格式标准,它可包含的信息有:姓名、地址资讯、电话号码、URL,logo,相片等。VCard模块提供了VCard能力,包括将VCard文件导入联系人数据库和将联系人数据导出为VCard文件等。
4
5>**说明:**
6>
7>本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。<br />
8>本模块为系统接口。
9
10## 导入模块
11
12```ts
13import { vcard } from '@kit.TelephonyKit';
14```
15
16## vcard.importVCard<sup>11+</sup>
17
18importVCard\(context: Context, filePath: string, callback: AsyncCallback\<void\>\): void
19
20将VCard文件导入联系人数据库。使用callback异步回调。
21
22**需要权限**:ohos.permission.WRITE_CONTACTSohos.permission.READ_CONTACTS
23
24**系统接口:** 此接口为系统接口。
25
26**系统能力**:SystemCapability.Telephony.CoreService
27
28**参数:**
29
30| 参数名   | 类型                        | 必填 | 说明                                   |
31| -------- | --------------------------- | ---- | -------------------------------------- |
32| context   | Context                      | 是   | 应用上下文。 |
33| filePath   | string                      | 是   | vcf文件地址。 |
34| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数,返回导入成功或失败的状态码。                             |
35
36**错误码:**
37
38以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
39
40| 错误码ID |                 错误信息                     |
41| -------- | -------------------------------------------- |
42| 201      | Permission denied.                           |
43| 202      | Non-system applications use system APIs.                           |
44| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
45| 8300001  | Invalid parameter value.                     |
46| 8300003  | System internal error.                       |
47| 8300999  | Unknown error.                               |
48
49**示例:**
50
51```ts
52import { window } from '@kit.ArkUI';
53import { UIAbility } from '@kit.AbilityKit';
54import { BusinessError } from '@kit.BasicServicesKit';
55import { vcard } from '@kit.TelephonyKit';
56
57class EntryAbility extends UIAbility {
58    onWindowStageCreate(windowStage: window.WindowStage) {
59        let filePath: string = "/data/storage/vcf/contacts.vcf";
60        vcard.importVCard(this.context, filePath, (err: BusinessError) => {
61            console.log(`callback: err->${JSON.stringify(err)}`);
62        });
63    }
64}
65
66```
67
68## vcard.importVCard<sup>11+</sup>
69
70importVCard\(context: Context,filePath: string, accountId: number, callback: AsyncCallback\<void\>\): void
71
72将VCard文件导入联系人数据库。使用callback异步回调。
73
74**需要权限**:ohos.permission.WRITE_CONTACTSohos.permission.READ_CONTACTS
75
76**系统接口:** 此接口为系统接口。
77
78**系统能力**:SystemCapability.Telephony.CoreService
79
80**参数:**
81
82| 参数名   | 类型                        | 必填 | 说明                                   |
83| -------- | --------------------------- | ---- | -------------------------------------- |
84| context   | Context                      | 是   | 应用上下文。 |
85| filePath   | string                      | 是   | vcf文件地址。 |
86| accountId   | number                      | 是   | 联系人账户ID。 |
87| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数,返回导入成功或失败的状态码。                             |
88
89**错误码:**
90
91以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
92
93| 错误码ID |                 错误信息                     |
94| -------- | -------------------------------------------- |
95| 201      | Permission denied.                           |
96| 202      | Non-system applications use system APIs.                           |
97| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
98| 8300001  | Invalid parameter value.                     |
99| 8300003  | System internal error.                       |
100| 8300999  | Unknown error.                               |
101
102**示例:**
103
104```ts
105import { window } from '@kit.ArkUI';
106import { UIAbility } from '@kit.AbilityKit';
107import { BusinessError } from '@kit.BasicServicesKit';
108import { vcard } from '@kit.TelephonyKit';
109
110class EntryAbility extends UIAbility {
111    onWindowStageCreate(windowStage: window.WindowStage) {
112        let filePath: string = "/data/storage/vcf/contacts.vcf";
113        let accountId: number = 0;
114        vcard.importVCard(this.context, filePath, accountId, (err: BusinessError) => {
115            console.log(`callback: err->${JSON.stringify(err)}`);
116        });
117    }
118}
119
120```
121
122## vcard.importVCard<sup>11+</sup>
123
124importVCard\(context: Context, filePath: string, accountId?: number\): Promise\<void\>
125
126将VCard文件导入联系人数据库。使用Promise异步回调。
127
128**需要权限**:ohos.permission.WRITE_CONTACTSohos.permission.READ_CONTACTS
129
130**系统接口:** 此接口为系统接口。
131
132**系统能力**:SystemCapability.Telephony.CoreService
133
134**参数:**
135
136| 参数名 | 类型   | 必填 | 说明                                   |
137| ------ | ------ | ---- | -------------------------------------- |
138| context   | Context                      | 是   | 应用上下文。|
139| filePath | string | 是   | vcf文件地址 。|
140| accountId   | number | 否   | 联系人账户ID。 |
141
142**返回值:**
143
144| 类型            | 说明                            |
145| --------------- | ------------------------------- |
146| Promise\<void\> | 以Promise形式异步返回结果。 |
147
148**错误码:**
149
150以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
151
152| 错误码ID |                 错误信息                     |
153| -------- | -------------------------------------------- |
154| 201      | Permission denied.                           |
155| 202      | Non-system applications use system APIs.                           |
156| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
157| 8300001  | Invalid parameter value.                     |
158| 8300003  | System internal error.                       |
159| 8300999  | Unknown error.                               |
160
161**示例:**
162
163```ts
164import { window } from '@kit.ArkUI';
165import { UIAbility } from '@kit.AbilityKit';
166import { BusinessError } from '@kit.BasicServicesKit';
167import { vcard } from '@kit.TelephonyKit';
168
169class EntryAbility extends UIAbility {
170    onWindowStageCreate(windowStage: window.WindowStage) {
171        let filePath: string = "/data/storage/vcf/contacts.vcf";
172        let accountId: number = 0;
173        vcard.importVCard(this.context, filePath, accountId).then(() => {
174            console.log(`importVCard success.`);
175        }).catch((err: BusinessError) => {
176            console.log(`importVCard failed, promise: err->${JSON.stringify(err)}`);
177        });
178    }
179}
180```
181
182## vcard.exportVCard<sup>11+</sup>
183
184exportVCard\(context: Context, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback\<string\>\): void
185
186将联系人导出为vcf文件。使用callback异步回调。
187
188**需要权限**:ohos.permission.WRITE_CONTACTSohos.permission.READ_CONTACTS
189
190**系统接口:** 此接口为系统接口。
191
192**系统能力**:SystemCapability.Telephony.CoreService
193
194**参数:**
195
196| 参数名   | 类型                        | 必填 | 说明                                   |
197| -------- | --------------------------- | ---- | -------------------------------------- |
198| context   | Context                      | 是   | 应用上下文。 |
199| predicates   | dataSharePredicates.DataSharePredicates | 是   | 查询语句。 |
200| callback | AsyncCallback&lt;string&gt; | 是   | 回调函数 callback表示生成的vcf文件地址。                             |
201
202**错误码:**
203
204以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
205
206| 错误码ID |                 错误信息                     |
207| -------- | -------------------------------------------- |
208| 201      | Permission denied.                           |
209| 202      | Non-system applications use system APIs.                           |
210| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
211| 8300001  | Invalid parameter value.                     |
212| 8300003  | System internal error.                       |
213| 8300999  | Unknown error.                               |
214
215**示例:**
216
217```ts
218import { window } from '@kit.ArkUI';
219import { UIAbility } from '@kit.AbilityKit';
220import { BusinessError } from '@kit.BasicServicesKit';
221import { vcard } from '@kit.TelephonyKit';
222import { dataSharePredicates } from '@kit.ArkData';
223
224class EntryAbility extends UIAbility {
225    onWindowStageCreate(windowStage: window.WindowStage) {
226        let predicates = new dataSharePredicates.DataSharePredicates();
227        predicates.equalTo("NAME", "Rose");
228
229        vcard.exportVCard(this.context, predicates, (err: BusinessError, data: string) => {
230            console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
231        });
232    }
233}
234
235```
236
237## vcard.exportVCard<sup>11+</sup>
238
239exportVCard\(context: Context, predicates: dataSharePredicates.DataSharePredicates, options: VCardBuilderOptions, callback: AsyncCallback\<string\>\): void
240
241将联系人导出为vcf文件。使用callback异步回调。
242
243**需要权限**:ohos.permission.WRITE_CONTACTSohos.permission.READ_CONTACTS
244
245**系统接口:** 此接口为系统接口。
246
247**系统能力**:SystemCapability.Telephony.CoreService
248
249**参数:**
250
251| 参数名   | 类型                        | 必填 | 说明                                   |
252| -------- | --------------------------- | ---- | -------------------------------------- |
253| context   | Context                      | 是   | 应用上下文。 |
254| predicates   | dataSharePredicates.DataSharePredicates | 是   | 查询语句。 |
255| options   | [VCardBuilderOptions](#vcardbuilderoptions11) | 是   | VCard版本与编码类型。 |
256| callback | AsyncCallback&lt;string&gt; | 是   | 回调函数 callback表示生成的vcf文件地址。                             |
257
258**错误码:**
259
260以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
261
262| 错误码ID |                 错误信息                     |
263| -------- | -------------------------------------------- |
264| 201      | Permission denied.                           |
265| 202      | Non-system applications use system APIs.                           |
266| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
267| 8300001  | Invalid parameter value.                     |
268| 8300003  | System internal error.                       |
269| 8300999  | Unknown error.                               |
270
271**示例:**
272
273```ts
274import { window } from '@kit.ArkUI';
275import { UIAbility } from '@kit.AbilityKit';
276import { BusinessError } from '@kit.BasicServicesKit';
277import { vcard } from '@kit.TelephonyKit';
278import { dataSharePredicates } from '@kit.ArkData';
279
280class EntryAbility extends UIAbility {
281    onWindowStageCreate(windowStage: window.WindowStage) {
282        let predicates = new dataSharePredicates.DataSharePredicates();
283        predicates.equalTo("NAME", "Rose");
284        let options: vcard.VCardBuilderOptions = {
285            cardType: vcard.VCardType.VERSION_21,
286            charset: "UTF-8"
287        };
288        vcard.exportVCard(this.context, predicates, options, (err: BusinessError, data: string) => {
289            console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
290        });
291    }
292}
293```
294
295## vcard.exportVCard<sup>11+</sup>
296
297exportVCard\(context: Context, predicates: dataSharePredicates.DataSharePredicates, options?: VCardBuilderOptions\): Promise\<string\>
298
299将联系人导出为vcf文件。使用Promise异步回调。
300
301**需要权限**:ohos.permission.WRITE_CONTACTSohos.permission.READ_CONTACTS
302
303**系统接口:** 此接口为系统接口。
304
305**系统能力**:SystemCapability.Telephony.CoreService
306
307**参数:**
308
309| 参数名 | 类型   | 必填 | 说明                                   |
310| ------ | ------ | ---- | -------------------------------------- |
311| context   | Context                      | 是   | 应用上下文。 |
312| predicates   | dataSharePredicates.DataSharePredicates | 是   | 查询语句。 |
313| options   | [VCardBuilderOptions](#vcardbuilderoptions11) | 否   | VCard版本与编码类型。 |
314
315**返回值:**
316
317| 类型            | 说明                            |
318| --------------- | ------------------------------- |
319| Promise\<string\> | 以Promise形式异步返回生成的vcf文件夹地址。 |
320
321**错误码:**
322
323以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
324
325| 错误码ID |                 错误信息                     |
326| -------- | -------------------------------------------- |
327| 201      | Permission denied.                           |
328| 202      | Non-system applications use system APIs.                           |
329| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
330| 8300001  | Invalid parameter value.                     |
331| 8300003  | System internal error.                       |
332| 8300999  | Unknown error.                               |
333
334**示例:**
335
336```ts
337import { window } from '@kit.ArkUI';
338import { UIAbility } from '@kit.AbilityKit';
339import { BusinessError } from '@kit.BasicServicesKit';
340import { vcard } from '@kit.TelephonyKit';
341import { dataSharePredicates } from '@kit.ArkData';
342
343class EntryAbility extends UIAbility {
344    onWindowStageCreate(windowStage: window.WindowStage) {
345        let predicates = new dataSharePredicates.DataSharePredicates();
346        predicates.equalTo("NAME", "Rose");
347        let options: vcard.VCardBuilderOptions = {
348            cardType: vcard.VCardType.VERSION_21,
349            charset: "UTF-8"
350        };
351        vcard.exportVCard(this.context, predicates, options).then(() => {
352            console.log(`exportVCard success.`);
353        }).catch((err: BusinessError) => {
354            console.log(`exportVCard failed, promise: err->${JSON.stringify(err)}`);
355        });
356    }
357}
358
359```
360
361## VCardBuilderOptions<sup>11+</sup>
362
363VCard版本和编码信息。
364
365**系统接口:** 此接口为系统接口。
366
367**系统能力**:SystemCapability.Telephony.CoreService
368
369| 名称         | 类型   | 必填 |    说明    |
370| ------------ | ------ | ---- | ---------- |
371| cardType     | [VCardType](#vcardtype11) |  否  | VCard版本类型 (默认值为VERSION_21)。     |
372| charset       | string |  否  | VCard编码类型(默认值为'UTF-8')。     |
373
374## VCardType<sup>11+</sup>
375
376VCard版本类型。
377
378**系统接口:** 此接口为系统接口。
379
380**系统能力**:SystemCapability.Telephony.CoreService
381
382| 名称            | 值   | 说明       |
383| --------------- | ---- | ---------- |
384| VERSION_21 | 0 | VCard2.1版本。 |
385| VERSION_30 | 1 | VCard3.0版本。 |
386| VERSION_40 | 2 | VCard4.0版本。 |
387