×
Namespaces

Variants
Actions

将vCard信息导入文件中

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
User:Jiangbaolan 在 01 Apr 2009 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

详细描述 下列示例演示如何将一个联系人信息从缺省的联系人数据库(作为vCard)导入到一个给定的文件中。 该代码可以自签名执行。

MMP文件 需要下列能力和链接库

Code: CAPABILITY ReadUserDataLIBRARY euser.lib LIBRARY estor.lib LIBRARY efsrv.lib LIBRARY cntmodel.lib源文件

Code:

  1. include <e32cmn.h> //TUid
  2. include <e32std.h> //User
  3. include <e32base.h> //CArrayPtr, CleanupStack
  4. include <e32def.h> //TBool
  5. include <s32file.h> //RFileReadStream
  6. include <f32file.h> //RFs
  7. include <cntdb.h> //CContactDatabase
  8. include <cntdef.h> //CContactIdArray
  9. include <cntfilt.h> //CCntFilterTBool ExportVCardL(const TDesC& aFileName, TInt aItemIndex)

{

 RFs fileSession;
 RFile file;
   
 User::LeaveIfError(fileSession.Connect());
 CleanupClosePushL(fileSession);

 if (file.Replace(fileSession, aFileName, EFileWrite) != KErrNone)
     {
     //failed to create the file
     CleanupStack::PopAndDestroy(); //fileSession
     return EFalse;
     }
 CleanupClosePushL(file);

 //open a write stream to the file
 RFileWriteStream outputFileStream(file);
 CleanupClosePushL(outputFileStream);

 //open the default contacts database
 CContactDatabase* contactsDb = CContactDatabase::OpenL();
 CleanupStack::PushL(contactsDb);

//create an array of contact IDs to export

 CContactIdArray* exportContacts = CContactIdArray::NewL();
 CleanupStack::PushL(exportContacts);
           
 //use a filter to get only contact items (e.g. templates are excluded)
 CCntFilter *filter = CCntFilter::NewLC();
 filter->SetContactFilterTypeALL(EFalse);
 filter->SetContactFilterTypeCard(ETrue);
 contactsDb->FilterDatabaseL(*filter);
           
 //create an array to hold all filtered contact items
 CContactIdArray* contactIds;
 contactIds = CContactIdArray::NewLC(filter->iIds);
   
 //add given contact(by index) to the array of contact IDs to export 
 if((*contactIds).Count() >= aItemIndex)
     exportContacts->AddL((*contactIds)[aItemIndex] );            
           
 CleanupStack::PopAndDestroy(2); //contactIds, filter
           
 //KVersitEntityUidVCard is used to identify a vCard
 TUid uid = TUid::Uid(KVersitEntityUidVCard);
 contactsDb->ExportSelectedContactsL(uid,
                                     *exportContacts,
                                      outputFileStream,
                                      //contact ID is no exported 
                                      CContactDatabase::EExcludeUid);

 CleanupStack::PopAndDestroy(5); //exportContacts,contactsDb, 
                 //outputFileStream,file,fileSession 
   
 return ETrue;

}

This page was last modified on 15 December 2011, at 09:34.
32 page views in the last 30 days.