gpt4 book ai didi

java - 如何为选定的联系人创建 .vcf 文件

转载 作者:行者123 更新时间:2023-12-01 15:32:52 26 4
gpt4 key购买 nike

我使用以下代码为单个联系人创建 vcf 文件。我想要的是,当我单击特定联系人时,它将为该联系人创建一个 vcf 文件。这是我正在使用的代码:

  String vfile = "test1.vcf";
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);


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 = 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 out = new FileOutputStream(path);
out.write(VCard.toString().getBytes());
Log.d("Vcard", VCard);
} catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();

}

如有任何帮助,我们将不胜感激。

最佳答案

嘿chandu仔细阅读我的帖子肯定对你有帮助首先获取您点击的人的联系ID之后调用这个函数

    public void getVcardString() {
// TODO Auto-generated method stub
System.out.println("i m in get vcardstring");
ArrayList<string> vCard = new ArrayList<String>();
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ "="+ShareContactActivity.user_contact_id, null, null);

cursor.moveToFirst();
for(int i =0;i<cursor.getCount();i++)
{

get(cursor);
System.out.println("i m in above vcf string");
System.out.println("TAG"+ "Contact "+(i+1)+"VcF String is"+vCard.get(i));
cursor.moveToNext();
}
}

public void get(Cursor cursor)
{
//cursor.moveToFirst();
FileInputStream fis=null;
System.out.println("i m in get cursor");
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");



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; // vfile is the variable of type string and hold value like vcf file name whatever name u give to this ariable it will save with that name.
filelocation=storage_path;
System.out.println("this is file location "+Environment.getExternalStorageDirectory().toString()+File.separator+vfile);
FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
mFileOutputStream.write(vcardstring.toString().getBytes());
System.out.println("i m in end of get vcardstring");

} catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally
{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

还在 list 文件中添加两个权限

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>

所以上面的代码将解决您的问题,如果您发现任何问题,请再次询问我。如果您的问题得到解决,请投票我的答案。

关于java - 如何为选定的联系人创建 .vcf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9359960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com