如何为所选联系人创建.vcf文件

时间:2012-02-20 10:55:00

标签: java android android-intent

我使用以下代码为单个联系人创建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();

        }

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

嘿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();
}
}
}

还在清单文件中添加两个权限

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

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