Android 以vcf格式提取联系人列表

Android 以vcf格式提取联系人列表,android,Android,如何运行android方法以vcf格式提取联系人列表? 是否存在可以直接调用此操作的意图 thx 弗朗索瓦您可以在联系人应用程序中手动执行此操作。但这并不是故意的。如果你想让你的应用程序有这样一个导出,不幸的是你必须自己编写或者使用这里的代码。这可能对你有帮助。根据你的需要试试这个- package com.vcard; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; i

如何运行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();
        }
    }
}
package com.vcard;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.res.AssetFileDescriptor;
导入android.database.Cursor;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Environment;
导入android.provider.contacts合同;
导入android.util.Log;
导入android.view.view;
公共类VCardActivity扩展了活动
{
光标;
ArrayList vCard;
字符串vfile;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vfile=“Contacts”+“\u”+System.currentTimeMillis()+”.vcf”;
/**此函数用于Vcard,这里我使用一个数组列表,其中存储每个Conatact的每个Vcard字符串
*这里我取一个游标,这个游标不是空的,它的计数>0,然后我重复一个循环到游标。getcount()表示最多有多少个电话联系人。
*在每个循环中,我可以生成vcard字符串并存储在数组列表中,我将其声明为全局数组。
*在每个循环中,我移动光标,然后在logcat中打印日志。
* */
getVcardString();
}
私有void getVcardString(){
//TODO自动生成的方法存根
vCard=新的ArrayList();
cursor=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null,null);
if(cursor!=null&&cursor.getCount()>0)
{
cursor.moveToFirst();

对于(int i=0;i以下是如何提取到VCF文件并通过intent共享它,如果您愿意,可以选择共享单个联系人:

    @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;
    }

自2016年9月起,情况已发生变化。您现在可以使用“联系人”应用程序将联系人导出到VCF文件:在菜单中选择“导入/导出”,然后选择其中一个导出项。您仍然需要从手机上获取该文件,但通过ADB(或电子邮件)很容易

我认为最好使用特定大小的缓冲区,而不是将其全部加载到字符串中,然后使用它。