1

I have copied the code from the below link to export contacts to a vcf file.it works fine but the problem is Every contact that has more than one phone number (a mobile and work number) are saved twice. And both of the numbers are in each duplicate contact, so they are getting duplicated. please help

Export the Contacts as VCF file

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);

phones.moveToFirst();
for(int i =0;i<phones.getCount();i++)
{
  String lookupKey =    phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);

AssetFileDescriptor fd;
 try 
 {

     fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");
     fis = fd.createInputStream();
     byte[] buf = new byte[(int) fd.getDeclaredLength()];
     fis.read(buf);
     String VCard = new String(buf);


     //private File mPath = new File(Environment.getExternalStorageDirectory() + "//yourdir//");
      sdCard =new File(Environment.getExternalStorageDirectory() + "//yourdir//");
      directory = new File (sdCard.getAbsolutePath());
     directory.mkdirs();
     file = new File(directory, vfile);




     // String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
     FileOutputStream mFileOutputStream = new FileOutputStream(file,true);
     mFileOutputStream.write(VCard.toString().getBytes());        

     phones.moveToNext(); 
    //Log.d("Vcard",  VCard);
 } 
 catch (Exception e1) 
 {
      // TODO Auto-generated catch block
      e1.printStackTrace();
 }

 }}
Toast.makeText(this, "contacts copied to  " + vfile, 
       Toast.LENGTH_SHORT).show();
Community
  • 1
  • 1
user000007
  • 11
  • 3

0 Answers0