Android的:如何将联系人保存到SD卡作为电子名片。没有重复?如何将、联系人、电子名片、Android

2023-09-07 13:06:29 作者:奶哟小仙女

我想在手机上保存的所有联系人的SD卡作为.vcf文件(电子名片)。它的工作原理,但我有一个问题。有多个电话号码(移动和工作号码)每一次接触都保存两次。和两个数字是在每个重复接触,所以它们是正确的,只是重复。是否有人可以告诉我如何解决这个问题?我的code是:

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


      }

感谢您的帮助。

推荐答案

使用低于$ C $下储存联系人数据作为VCF文件到设备的SD卡。

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.

导出联系人作为VCF文件

和在以下权限到的Andr​​oidManifest.xml 文件。

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