android vcf格式化,Android Contact 导入导出 vcf格式(不依赖第三方库)

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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值