重复的联系人作为vcf文件android

时间:2017-05-27 18:36:37

标签: java android

我在论坛周围找到了这个代码,用于创建一个包含手机中存储的所有联系人的vcf文件。好吧,我得到所有联系人,但重复有谁知道如何解决它?我只对联系人书中显示的联系人感兴趣而不是谷歌联系人等。谢谢

  private void getVcardString() throws IOException {
            // TODO Auto-generated method stub
            //ProgressBar pro = (ProgressBar)findViewById(R.id.pb);

            // ProgressBar pro = (ProgressBar) findViewById(R.id.pb1);
            vCard = new ArrayList<String>();  // Its global....
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1",
                    null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                int i;
                String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
                FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
                cursor.moveToFirst();
                for (i = 0; i < cursor.getCount(); i++) {

                    get(cursor);
                    Log.d("TAG", "Contact " + (i + 1) + "VcF String is" + vCard.get(i));
                    cursor.moveToNext();

                    mFileOutputStream.write(vCard.get(i).toString().getBytes());
                }
                mFileOutputStream.close();
                cursor.close();
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        tx.setText("");
                        pro.setVisibility(View.GONE);
                        btn3.setVisibility(View.VISIBLE);
                        tx1.setVisibility(View.VISIBLE);
                        Toast toast=Toast.makeText(getApplicationContext(),"בוצע גיבוי לכרטיס הזיכרון",Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0,90);
                        toast.show();

                    }
                });




            } else {
                Log.d("TAG", "No Contacts in Your Phone");
            }
        }

        private void get(Cursor cursor2) {
            String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = getContentResolver().openAssetFileDescriptor(uri, "r");

                FileInputStream fis = fd.createInputStream();
                byte[] buf = new byte[(int) fd.getDeclaredLength()];
                fis.read(buf);
                String vcardstring = new String(buf);
                vCard.add(vcardstring);

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    });
} here

1 个答案:

答案 0 :(得分:0)

刚刚找到解决方案编辑查询:

getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);