Skip to content Skip to sidebar Skip to footer

Extract Contact List In Vcf Format

How could i run the android method that extract the contact list in a vcf format? Is there an intent that can directly call this action? thx François

Solution 1:

This may be helpful to you. Try this with your need -

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;

publicclassVCardActivityextendsActivity 
{
    Cursor cursor;
    ArrayList<String> vCard ;
    String vfile;

    /** Called when the activity is first created. */@OverridepublicvoidonCreate(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();

    }
    privatevoidgetVcardString() {
        // TODO Auto-generated method stub
        vCard = newArrayList<String>();
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        if(cursor!=null&&cursor.getCount()>0)
        {
            cursor.moveToFirst();
            for(inti=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");
        }

    }

    publicvoidget(Cursor cursor)
    {
        //cursor.moveToFirst();StringlookupKey= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
        Uriuri= 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);*/FileInputStreamfis= fd.createInputStream();
            byte[] buf = newbyte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String vcardstring= newString(buf);
            vCard.add(vcardstring);

            Stringstorage_path= Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
            FileOutputStreammFileOutputStream=newFileOutputStream(storage_path, false);
            mFileOutputStream.write(vcardstring.toString().getBytes());

        } catch (Exception e1) 
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

Solution 2:

Here's how to extract to VCF file and share it via an intent, with the option of sharing a single contact if you wish:

@WorkerThread@Nullablepublicstatic Intent getContactShareIntent(@NonNull Context context, @Nullable String contactId, @NonNull String filePath) {
        finalContentResolvercontentResolver= context.getContentResolver();
        Cursorcursor= 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 + "=?", newString[]{contactId}, null);
        if (cursor == null || !cursor.moveToFirst())
            returnnull;
        finalFilefile=newFile(filePath);
        file.delete();
        do {
            StringlookupKey= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uriuri= Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = contentResolver.openAssetFileDescriptor(uri, "r");
                if (fd == null)
                    returnnull;
                FileInputStreamfis= fd.createInputStream();
                IOUtils.copy(fis, newFileOutputStream(filePath, false));
                cursor.moveToNext();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } while (cursor.moveToNext());
        Intentintent=newIntent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("*/*");
        return intent;
    }

Solution 3:

You can do this manually in the contacts App. But there is no Intent for that. If you want your App to have such an export you unfortunately have to write it yourself or use code from here.vcardio

Solution 4:

This has changed as of Sept., 2016. You can now export your contacts to a VCF file using the 'People' app: select 'Import/export' in the menu, then one of the export items. You'll still have to get the file off your phone but that's easy enough via ADB (or maybe email.)

Post a Comment for "Extract Contact List In Vcf Format"