2012-02-20 56 views
0

我正在使用以下代码为单个联系人创建vcf文件。我想要的是,当我点击特定联系人时,它会为该联系人创建一个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(); 

     } 

任何帮助,将不胜感激。

+0

帮我 “智能灰” http://stackoverflow.com/questions/13198933/cant-find-vcf-file-on-sdcard-android-emulator/13199144#comment17969669_13199144 感谢提前 – 2012-11-02 16:53:41

回答

1

哎chandu读它完全不在乎我的文章肯定帮你 首先得到其中U单击 后,该人的接触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> 

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

+0

感谢ashutosh它为我工作,并始终张贴您的信息在堆栈中。它帮助我们。 – itechDroid 2012-04-18 12:04:26