openharmony 鸿蒙 js-apis-vcard-sys

2025-06-12 浏览 (1)

@ohos.telephony.vcard (VCard) (System API)

VCard is a file format standard for electronic business cards. It contains information such as names, addresses, phone numbers, URLs, logos, and photos. The VCard module provides the VCard management functions, including importing VCard files to the contact database and exporting contact data to VCard files.

NOTE

The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
The APIs provided by this module are system APIs.

Modules to Import

import { vcard } from '@kit.TelephonyKit';

vcard.importVCard11+

importVCard(context: Context, filePath: string, callback: AsyncCallback<void>): void

Imports a VCard file (that is, .vcf file) to the contact database. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.WRITE_CONTACTS and ohos.permission.READ_CONTACTS

System API: This is a system API.

System capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
filePathstringYesAddress of the .vcf file.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see ohos.telephony (Telephony) Error Codes.

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300003System internal error.
8300999Unknown error.

Example

import { window } from '@kit.ArkUI';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { vcard } from '@kit.TelephonyKit';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
        let filePath: string = "/data/storage/vcf/contacts.vcf";
        vcard.importVCard(this.context, filePath, (err: BusinessError) => {
            console.log(`callback: err->${JSON.stringify(err)}`);
        });
    }
}

vcard.importVCard11+

importVCard(context: Context,filePath: string, accountId: number, callback: AsyncCallback<void>): void

Imports a VCard file (that is, .vcf file) to the contact database. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.WRITE_CONTACTS and ohos.permission.READ_CONTACTS

System API: This is a system API.

System capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
filePathstringYesAddress of the .vcf file.
accountIdnumberYesContact account ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see ohos.telephony (Telephony) Error Codes.

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300003System internal error.
8300999Unknown error.

Example

import { window } from '@kit.ArkUI';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { vcard } from '@kit.TelephonyKit';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
        let filePath: string = "/data/storage/vcf/contacts.vcf";
        let accountId: number = 0;
        vcard.importVCard(this.context, filePath, accountId, (err: BusinessError) => {
            console.log(`callback: err->${JSON.stringify(err)}`);
        });
    }
}

vcard.importVCard11+

importVCard(context: Context, filePath: string, accountId?: number): Promise<void>

Imports a VCard file (that is, .vcf file) to the contact database. This API uses a promise to return the result.

Required permissions: ohos.permission.WRITE_CONTACTS and ohos.permission.READ_CONTACTS

System API: This is a system API.

System capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
filePathstringYesAddress of the .vcf file.
accountIdnumberNoContact account ID.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see ohos.telephony (Telephony) Error Codes.

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300003System internal error.
8300999Unknown error.

Example

import { window } from '@kit.ArkUI';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { vcard } from '@kit.TelephonyKit';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
        let filePath: string = "/data/storage/vcf/contacts.vcf";
        let accountId: number = 0;
        vcard.importVCard(this.context, filePath, accountId).then(() => {
            console.log(`importVCard success.`);
        }).catch((err: BusinessError) => {
            console.log(`importVCard failed, promise: err->${JSON.stringify(err)}`);
        });
    }
}

vcard.exportVCard11+

exportVCard(context: Context, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<string>): void

Exports contacts to a .vcf file. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.WRITE_CONTACTS and ohos.permission.READ_CONTACTS

System API: This is a system API.

System capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
predicatesdataSharePredicates.DataSharePredicatesYesQuery statement.
callbackAsyncCallback<string>YesCallback used to return the result, which is the address of the .vcf file.

Error codes

For details about the error codes, see ohos.telephony (Telephony) Error Codes.

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300003System internal error.
8300999Unknown error.

Example

import { window } from '@kit.ArkUI';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { vcard } from '@kit.TelephonyKit';
import { dataSharePredicates } from '@kit.ArkData';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
        let predicates = new dataSharePredicates.DataSharePredicates();
        predicates.equalTo("NAME", "Rose");

        vcard.exportVCard(this.context, predicates, (err: BusinessError, data: string) => {
            console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
        });
    }
}

vcard.exportVCard11+

exportVCard(context: Context, predicates: dataSharePredicates.DataSharePredicates, options: VCardBuilderOptions, callback: AsyncCallback<string>): void

Exports contacts to a .vcf file. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.WRITE_CONTACTS and ohos.permission.READ_CONTACTS

System API: This is a system API.

System capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
predicatesdataSharePredicates.DataSharePredicatesYesQuery statement.
optionsVCardBuilderOptionsYesVCard version and encoding type.
callbackAsyncCallback<string>YesCallback used to return the result, which is the address of the .vcf file.

Error codes

For details about the error codes, see ohos.telephony (Telephony) Error Codes.

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300003System internal error.
8300999Unknown error.

Example

import { window } from '@kit.ArkUI';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { vcard } from '@kit.TelephonyKit';
import { dataSharePredicates } from '@kit.ArkData';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
        let predicates = new dataSharePredicates.DataSharePredicates();
        predicates.equalTo("NAME", "Rose");
        let options: vcard.VCardBuilderOptions = {
            cardType: vcard.VCardType.VERSION_21,
            charset: "UTF-8"
        };
        vcard.exportVCard(this.context, predicates, options, (err: BusinessError, data: string) => {
            console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
        });
    }
}

vcard.exportVCard11+

exportVCard(context: Context, predicates: dataSharePredicates.DataSharePredicates, options?: VCardBuilderOptions): Promise<string>

Exports contacts to a .vcf file. This API uses a promise to return the result.

Required permissions: ohos.permission.WRITE_CONTACTS and ohos.permission.READ_CONTACTS

System API: This is a system API.

System capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
predicatesdataSharePredicates.DataSharePredicatesYesQuery statement.
optionsVCardBuilderOptionsNoVCard version and encoding type.

Return value

TypeDescription
Promise<string>Promise used to return the result, which is the address of the .vcf file.

Error codes

For details about the error codes, see ohos.telephony (Telephony) Error Codes.

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300003System internal error.
8300999Unknown error.

Example

import { window } from '@kit.ArkUI';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { vcard } from '@kit.TelephonyKit';
import { dataSharePredicates } from '@kit.ArkData';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
        let predicates = new dataSharePredicates.DataSharePredicates();
        predicates.equalTo("NAME", "Rose");
        let options: vcard.VCardBuilderOptions = {
            cardType: vcard.VCardType.VERSION_21,
            charset: "UTF-8"
        };
        vcard.exportVCard(this.context, predicates, options).then(() => {
            console.log(`exportVCard success.`);
        }).catch((err: BusinessError) => {
            console.log(`exportVCard failed, promise: err->${JSON.stringify(err)}`);
        });
    }
}

VCardBuilderOptions11+

Defines the VCard information.

System API: This is a system API.

System capability: SystemCapability.Telephony.CoreService

NameTypeMandatoryDescription
cardTypeVCardTypeNoVCard version. The default value is VERSION_21.
charsetstringNoVCard encoding type. The default value is UTF-8.

VCardType11+

Enumerates VCard versions.

System API: This is a system API.

System capability: SystemCapability.Telephony.CoreService

NameValueDescription
VERSION_210VCard 2.1.
VERSION_301VCard 3.0.
VERSION_402VCard 4.0.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Telephony Kit

harmony 鸿蒙Telephony_NetworkState

harmony 鸿蒙Telephony Error Codes

harmony 鸿蒙js-apis-call-sys

harmony 鸿蒙@ohos.telephony.call (Call)

harmony 鸿蒙@ohos.telephony.esim (eSIM Management) (System API)

harmony 鸿蒙@ohos.telephony.esim (eSIM Management)

harmony 鸿蒙@ohos.telephony.observer (Observer) (System API)

harmony 鸿蒙@ohos.telephony.observer (Observer)

harmony 鸿蒙@ohos.telephony.radio (Radio) (System API)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/IceN2Y