packagecom.mygmer.contactstest;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.util.ArrayList;importandroid.app.Activity;importandroid.content.res.AssetFileDescriptor;importandroid.database.Cursor;importandroid.net.Uri;importandroid.os.Bundle;importandroid.os.Environment;importandroid.provider.ContactsContract;importandroid.util.Log;public class VCardActivity extendsActivity {
Cursor cursor;
ArrayListvCard;
String vfile;/*** Called when the activity is first created.*/@Overridepublic voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vfile= "Contacts" + "_" + "mfTest" + ".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 voidgetVcardString() {//TODO Auto-generated method stub
vCard = new ArrayList();
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 voidget(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= newString(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();
}
}
}