我想将已检查的联系人复制到SD卡作为vcf文件
有没有办法将已检查的联系人从列表视图复制到SD卡?我已经制作了一个应用程序,我可以从联系人菜单中导出联系人,但我想从列表视图中导出联系人,并检查联系人。请帮助。我必须提交一个项目。
itemsSelected = "Selected items: \n";
for (int i = 0; i < lstContacts.getCount(); i++) {
view = lstContacts.getChildAt(i);
check= (CheckBox)view.findViewById(R.id.cb2);
if (check.isChecked()) {
itemsSelected += lstContacts.getItemAtPosition(i) + "\n";
}
}
答案 0 :(得分:0)
最后我得到了这个问题的答案。 在这里,lv是listview的联系人列表。只需复制此代码,您就可以检查要复制到SD卡的联系人。
long [] ids = lv.getCheckedItemIds();
for(long id : ids) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, 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 = this.getContentResolver().openAssetFileDescriptor(uri, "r");
fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String VCard = new String(buf);
//private File mPath = new File(Environment.getExternalStorageDirectory() + "//yourdir//");
sdCard =new File(Environment.getExternalStorageDirectory() + "//yourdir//");
directory = new File (sdCard.getAbsolutePath());
directory.mkdirs();
file = new File(directory, vfile);
// String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
FileOutputStream mFileOutputStream = new FileOutputStream(file,true);
mFileOutputStream.write(VCard.toString().getBytes());
phones.moveToNext();
//Log.d("Vcard", VCard);
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}}
Toast.makeText(this, "contacts copied to " + vfile,
Toast.LENGTH_SHORT).show();