如何为单个联系人创建 .vcf 文件?
How can I create .vcf file for a single contact?
我已经成功地为我的 android 设备上的所有联系人创建了 .vcf 文件。我在下面提到 link 这样做:
Export the Contacts as VCF file
而且效果很好。
但是,我需要根据我的要求将单个联系人转换为单个 .vcf 文件。
在此当前代码中,使用以下行获取所有联系人:
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
然后所有联系人将被覆盖在 .vcf 文件中。
那么,我可以为单个联系人生成 .vcf 文件的方法是什么?
查看下面代码块中的第一行。这里我每次都创建一个新的vFile,这个方法将called.So每个联系人将保存在不同的文件中。
public void get(Cursor cursor)
{
vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";
//cursor.moveToFirst();
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try {
fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");
// Your Complex Code and you used function without loop so how can you get all Contacts Vcard.??
/* 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 out = new FileOutputStream(path);
out.write(VCard.toString().getBytes());
Log.d("Vcard", VCard);*/
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String vcardstring= new String(buf);
vCard.add(vcardstring);
String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
mFileOutputStream.write(vcardstring.toString().getBytes());
} catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
我已经成功地为我的 android 设备上的所有联系人创建了 .vcf 文件。我在下面提到 link 这样做:
Export the Contacts as VCF file
而且效果很好。 但是,我需要根据我的要求将单个联系人转换为单个 .vcf 文件。
在此当前代码中,使用以下行获取所有联系人:
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
然后所有联系人将被覆盖在 .vcf 文件中。
那么,我可以为单个联系人生成 .vcf 文件的方法是什么?
查看下面代码块中的第一行。这里我每次都创建一个新的vFile,这个方法将called.So每个联系人将保存在不同的文件中。
public void get(Cursor cursor)
{
vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";
//cursor.moveToFirst();
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try {
fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");
// Your Complex Code and you used function without loop so how can you get all Contacts Vcard.??
/* 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 out = new FileOutputStream(path);
out.write(VCard.toString().getBytes());
Log.d("Vcard", VCard);*/
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String vcardstring= new String(buf);
vCard.add(vcardstring);
String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
mFileOutputStream.write(vcardstring.toString().getBytes());
} catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}