首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将联系人复制为vcf文件android

将联系人复制为vcf文件android
EN

Stack Overflow用户
提问于 2017-05-28 02:36:37
回答 1查看 49关注 0票数 0

我在论坛上找到了这段代码,用于创建一个vcf文件,其中所有联系人都存储在手机中。好吧,我得到了所有的联系人,但重复的,有人知道如何修复它吗?我只对通讯录中显示的联系人感兴趣,而不是谷歌联系人等。谢谢

代码语言:javascript
复制
  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
EN

回答 1

Stack Overflow用户

发布于 2017-05-29 02:53:27

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

代码语言:javascript
复制
getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44220403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档