以vcf格式提取联系人列表

问题描述:

如何运行提取vcf格式联系人列表的android方法?
有没有可以直接调用这个动作的意图?以vcf格式提取联系人列表

THX 弗朗索瓦

这可能会对您有所帮助。你需要尝试这一点 -

package com.vcard; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.ArrayList; 

import android.app.Activity; 
import android.content.res.AssetFileDescriptor; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.view.View; 

public class VCardActivity extends Activity 
{ 
    Cursor cursor; 
    ArrayList<String> vCard ; 
    String vfile; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf"; 
     /**This Function For Vcard And here i take one Array List in Which i store every Vcard String of Every Conatact 
     * Here i take one Cursor and this cursor is not null and its count>0 than i repeat one loop up to cursor.getcount() means Up to number of phone contacts. 
     * And in Every Loop i can make vcard string and store in Array list which i declared as a Global. 
     * And in Every Loop i move cursor next and print log in logcat. 
     * */ 
     getVcardString(); 

    } 
    private void getVcardString() { 
     // TODO Auto-generated method stub 
     vCard = new ArrayList<String>(); 
     cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
     if(cursor!=null&&cursor.getCount()>0) 
     { 
      cursor.moveToFirst(); 
      for(int i =0;i<cursor.getCount();i++) 
      { 

       get(cursor); 
       Log.d("TAG", "Contact "+(i+1)+"VcF String is"+vCard.get(i)); 
       cursor.moveToNext(); 
      } 

     } 
     else 
     { 
      Log.d("TAG", "No Contacts in Your Phone"); 
     } 

    } 

    public void get(Cursor cursor) 
    { 
     //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(); 
     } 
    } 
} 
+0

我认为最好使用特定大小的缓冲区而不是全部加载成一个字符串然后使用它。 – 2015-11-09 10:40:35

+0

这不是7.0以上的工作 – KishanSolanki124 2018-01-17 07:25:19

您可以在联系人应用程序手动完成。但是没有这个意图。如果你想要你的应用程序有这样的出口,不幸的是你必须自己写或使用这里的代码。 vcardio

+0

ok Thommy thx答案 – 2012-01-02 20:12:52

以下是如何解压到VCF文件,并通过意图分享,以期分享一个联系人的选项,如果你想:

@WorkerThread 
    @Nullable 
    public static Intent getContactShareIntent(@NonNull Context context, @Nullable String contactId, @NonNull String filePath) { 
     final ContentResolver contentResolver = context.getContentResolver(); 
     Cursor cursor = TextUtils.isEmpty(contactId) ? contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null) : 
       contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, Data.CONTACT_ID + "=?", new String[]{contactId}, null); 
     if (cursor == null || !cursor.moveToFirst()) 
      return null; 
     final File file = new File(filePath); 
     file.delete(); 
     do { 
      String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
      Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
      AssetFileDescriptor fd; 
      try { 
       fd = contentResolver.openAssetFileDescriptor(uri, "r"); 
       if (fd == null) 
        return null; 
       FileInputStream fis = fd.createInputStream(); 
       IOUtils.copy(fis, new FileOutputStream(filePath, false)); 
       cursor.moveToNext(); 
      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
     } while (cursor.moveToNext()); 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
     intent.setType("*/*"); 
     return intent; 
    } 

这已经改变您现在可以使用“人员”应用将您的联系人导出为VCF文件:在菜单中选择“导入/导出”,然后选择其中一个导出项目。你仍然需要将文件从你的手机中取出,但通过亚行(或者电子邮件)很容易。