【问题标题】:How to remove duplicate contacts in vcf file如何删除vcf文件中的重复联系人
【发布时间】:2014-09-14 12:19:26
【问题描述】:

大家好,我有这个代码,我用它从手机中获取联系人并将它们存储为 vcf 格式,该代码适用于只有一个号码的联系人,但我不断收到具有多个号码的联系人的重复,即有多个号码的联系人家庭号码,工作号码等..任何帮助将不胜感激.....这是我下面的代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;


    public class Show_contacts 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.activity_main);
                mContext = Show_contacts.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);
                        mFileOutputStream.close();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        }

【问题讨论】:

    标签: android android-contacts duplicate-removal vcf-vcard


    【解决方案1】:

    我们必须检查lookupKey。它对我有用。

    Cursor cursor;
    ArrayList<String> vCard;
    Context mContext;
    ArrayList<String> saveLkKey;
    String vcfFile;
    String currentTime;
    String rootStorage;
    
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.save_contact_fragment, container,
                false);
    
        mContext = getActivity();
        rootStorage = Environment.getExternalStorageDirectory() + "/";
    
        Time today = new Time(Time.getCurrentTimezone());
        today.setToNow();
    
        currentTime = today.monthDay + "" + (today.month + 1) + "" + today.year
                + "-" + today.format("%k%M%S") + "";
    
        vcfFile = "myContacts" + currentTime + ".vcf";
    
    
        Button btnSaveVcf = (Button) view.findViewById(R.id.btnSave1);
        btnSaveVcf.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                getVCF(vcfFile);
    
            }
    
        });
    
    return view;
    }
    
    public void getVCF(String vfile) {
    
        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));
            lookupKey = lookupKey.trim();
    
            if (saveLkKey == null) {
                saveLkKey = new ArrayList<String>();
                saveLkKey.add(lookupKey);
            } else {
                if (isDuplicate(lookupKey)) {
    
                    phones.moveToNext();
                    continue;
                } else {
                    saveLkKey.add(lookupKey);
                }
            }
    
            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 = rootStorage + vfile;
    
                FileOutputStream mFileOutputStream = new FileOutputStream(path,
                        true);
                mFileOutputStream.write(VCard.toString().getBytes());
                phones.moveToNext();
                Log.d("Vcard", VCard);
                mFileOutputStream.close();
    
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    
    
    }
    
    public boolean isDuplicate(String lkKey) {
        for (String s : saveLkKey) {
            if (s.equals(lkKey)) {
                return true;
            }
        }
        return false;
    }
    

    【讨论】:

    • 2 年后才回来查看这个问题,不幸的是我已经停止了 android 开发。同样感谢您的回答:)
    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多