Android:如何将联系人保存为SD卡作为vCard。没有重复?

时间:2012-08-21 00:19:12

标签: android duplicates contacts vcard

我正在尝试将手机上的所有联系人保存为SD卡作为.vcf文件(vCard)。它有效,但我有一个问题。每个具有多个电话号码(移动电话和工作号码)的联系人都会保存两次。并且这两个数字都在每个重复的联系人中,因此它们是正确的,只是重复。有人可以告诉我如何解决这个问题?我的代码是:

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();
        }


      }

感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

使用以下代码将联系人数据作为vcf文件保存到设备SD卡中。

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();
                }
            }
        }
    }

并查看以下链接以获取更多信息。

Export Contacts as a VCF file

并将以下权限授予您的androidmanifest.xml文件。

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

答案 1 :(得分:2)

检查 lookupKey ...

的双重性