Android: How to save contacts to sdcard as vCard. Without duplicates?

被刻印的时光 ゝ 提交于 2019-12-04 04:18:38

问题


I am trying to save all of the contacts on a phone to the sdcard as a .vcf file (vCard). It works, but I have a problem. 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 correct, just duplicated. Can someone please tell me how to fix this problem? My code is:

File delete=new File(Environment.getExternalStorageDirectory()+"/Contacts.vcf");

     if (delete.exists()) {
       delete.delete();

     }

    Cursor phones = ContactService.this.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 = ContactService.this.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();             
        } 
        catch (Exception e1) 
        {
             // TODO Auto-generated catch block
             e1.printStackTrace();
        }


      }

Thank you for your help.


回答1:


Use below code for Save contact data as a vcf file into device sdcard.

public class VCardActivity extends Activity {
    Cursor cursor;
    ArrayList<String> vCard;
    String vfile;
    static Context mContext;

    /** Called when the activity is first created. */
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mContext = VCardActivity.this;
            getVCF();
        }

        public static void getVCF() {
            final String vfile = "Contacts.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();
                }
            }
        }
    }

and see below link for more information.

Export Contacts as a VCF file

and give below permission into your androidmanifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



回答2:


Check duplicity for lookupKey...



来源:https://stackoverflow.com/questions/12046936/android-how-to-save-contacts-to-sdcard-as-vcard-without-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!