3

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

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


      }

谢谢您的帮助。

4

2 回答 2

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

并查看下面的链接了解更多信息。

将联系人导出为 VCF 文件

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-08-21T04:57:51.997 回答
2

检查lookupKey的重复性...

于 2012-08-28T11:00:14.767 回答