How to import or paste contacts from .vcf file into Android programmatically?

I am creating a file of .vcf

all contacts on Android using the below code.

public static void getVCF() 
    final String vfile = "POContactsRestore.vcf";
    Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    null, null, null);
    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;
             fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
             FileInputStream fis = fd.createInputStream();
             byte[] buf = new byte[(int) fd.getDeclaredLength()];
             String VCard = new String(buf);
             String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
             FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
             Log.d("Vcard",  VCard);
         catch (Exception e1) 
              // TODO Auto-generated catch block


How to add / restore contacts from .vcf

file programmatically in Android?

I want to restore all contact forms that are .vcf

on Android using code. How to do it?


source to share

1 answer

Actually, I want to do the same thing too, and after a lot of research I finally got a solution.

Here's a snippet of recovery code:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(storage_path+vfile)),"text/x-vcard"); //storage path is path of your vcf file and vFile is name of that file.


Enjoy !!



All Articles