如何删除vcf文件中的重复联系人
Posted
技术标签:
【中文标题】如何删除vcf文件中的重复联系人【英文标题】:How to remove duplicate contacts in vcf file 【发布时间】: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();
【问题讨论】:
【参考方案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 开发。同样感谢您的回答:)以上是关于如何删除vcf文件中的重复联系人的主要内容,如果未能解决你的问题,请参考以下文章
如何从网站将 vCard(.vcf 文件)导入 Android 联系人
如何使用 ListView 删除 Android Studio 显示中的重复联系人 [重复]
如何删除以编程方式添加新联系人时添加的 Android 联系人应用程序中的重复条目?
Android 如何发送多个联系人附加在单个 .vcf 文件中并发送到邮件?