Android sdk 支持vcf处理的(忘记最低哪个版本开始支持的了,可以查一查)
备注:此代码来自Stack Overflow(原地址找不到了,o(╥﹏╥)o)
1. 导出联系人为vcf格式
Contact provider中有ContactsContract.Contacts.CONTENT_VCARD_URI的定义,说明他是支持vCard规范的
package com.mygmer.contactstest; 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; 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.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 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(); } } }
2.导入vcf格式联系人
Example code:
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File("/storage/sdcard0/contacts_20180106150125.vcf")),"text/x-vcard"); startActivity(intent);
关于Intent.ACTION_VIEW的描述,里面举例就是举的Contact,嘿嘿:
/** * Activity Action: Display the data to the user. This is the most common * action performed on data -- it is the generic action you can use on * a piece of data to get the most reasonable thing to occur. For example, * when used on a contacts entry it will view the entry; when used on a * mailto: URI it will bring up a compose window filled with the information * supplied by the URI; when used with a tel: URI it will invoke the * dialer. * <p>Input: {@link #getData} is URI from which to retrieve data. * <p>Output: nothing. */
由上面的描述猜测,还原过程中会弹出提示框(会不会有啥确认按钮?),在Android5.1测试,有弹出窗,但没确认按钮,支持就导入联系人了。其他高版本未测试过