Как программно импортировать или вставлять контакты из файла .vcf в Android? - PullRequest
3 голосов
/ 07 марта 2012

Я создаю .vcf файл всех контактов в Android, используя следующий код.

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

Как добавить / восстановить контакты из файла .vcf программно в Android?

Я хочувосстановить все контакты из этого .vcf файла в Android с помощью кода.Как это сделать?

1 Ответ

9 голосов
/ 06 сентября 2012

На самом деле я тоже хочу сделать то же самое, и после долгого исследования, наконец, я нашел решение.

Вот фрагмент кода для восстановления:

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.
startActivity(intent);

Наслаждайтесь !!

...