openharmony 鸿蒙 js-apis-vcard

2026-08-25 浏览 (1)

@ohos.telephony.vcard (VCard)

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 23. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

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

vcard.importVCard

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 capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
filePathstringYesURL of the vcard file (VCF).
accountIdnumberYesContact account ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes:

For details about the error codes, see Telephony Error Codes.

IDError Message
201Permission denied.
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.error(`callback: err->${JSON.stringify(err)}`);
        });
    }
}

vcard.importVCard

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 capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
filePathstringYesURL of the vcard file (VCF).
accountIdnumberNoContact account ID.

Return value

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

Error codes:

For details about the error codes, see Telephony Error Codes.

IDError Message
201Permission denied.
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.info(`importVCard success.`);
        }).catch((err: BusinessError) => {
            console.error(`importVCard failed, promise: err->${JSON.stringify(err)}`);
        });
    }
}

vcard.importVCard

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 capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
filePathstringYesURL of the vcard file (VCF).
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes:

For details about the error codes, see Telephony Error Codes.

IDError Message
201Permission denied.
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.error(`callback: err->${JSON.stringify(err)}`);
        });
    }
}

vcard.exportVCard

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

Exports contacts as a vcard file (VCF). This API uses an asynchronous callback to return the result.

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

System capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
predicatesdataSharePredicates.DataSharePredicatesYesQuery statement.
optionsVCardBuilderOptionsYesVCard version and encoding type.
callbackAsyncCallback<string>YesCallback used to Address of the generated vcard file (VCF).

Error codes:

For details about the error codes, see Telephony Error Codes.

IDError Message
201Permission denied.
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.error(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
        });
    }
}

vcard.exportVCard

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

Exports contacts as a vcard file (VCF). This API uses a promise to return the result.

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

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 operation result.

Error codes:

For details about the error codes, see Telephony Error Codes.

IDError Message
201Permission denied.
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.info(`exportVCard success.`);
        }).catch((err: BusinessError) => {
            console.error(`exportVCard failed, promise: err->${JSON.stringify(err)}`);
        });
    }
}

vcard.exportVCard

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

Exports contacts as a vcard file (VCF). This API uses an asynchronous callback to return the result.

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

System capability: SystemCapability.Telephony.CoreService

Parameters

NameTypeMandatoryDescription
contextContextYesApplication context.
predicatesdataSharePredicates.DataSharePredicatesYesQuery statement.
callbackAsyncCallback<string>YesCallback used to Address of the generated vcard file (VCF).

Error codes:

For details about the error codes, see Telephony Error Codes.

IDError Message
201Permission denied.
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.error(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
        });
    }
}

VCardBuilderOptions

Defines the VCard information.

System capability: SystemCapability.Telephony.CoreService

NameTypeRead-OnlyOptionalDescription
cardTypeVCardTypeNoYesVCard version. The default value is VERSION_21.
charsetstringNoYesVCard encoding type. The default value is UTF-8.

VCardType

Enumerates VCard versions.

System capability: SystemCapability.Telephony.CoreService

NameValueDescription
VERSION_210VCard 2.1.
VERSION_301VCard 3.0.
VERSION_402VCard 4.0.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 js-apis-sms-sys

openharmony 鸿蒙 capi-telephony-radio-type-h

openharmony 鸿蒙 js-apis-sim-sys

openharmony 鸿蒙 js-apis-telephony-data-sys

openharmony 鸿蒙 js-apis-observer-sys

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 capi-telephony-data-h

openharmony 鸿蒙 ndk-apis-telephony-radio

openharmony 鸿蒙 capi-telephony-radio-h

openharmony 鸿蒙 js-apis-call

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