我通过添加手机,电子邮件和照片等详细信息手动创建vcf文件。我正在关注vcf格式3.0
FileOutputStream fileOutputStream = new FileOutputStream(PATH, true);
for (VCard vcard : vcards) {
StringBuilder contactsList = new StringBuilder();
String name = vcard.getFormattedName().getValue();
contactsList.append("BEGIN:VCARD\r\n");
contactsList.append("VERSION:3.0\r\n");
contactsList.append("N:" + name + "\r\n");
contactsList.append("TEL;TYPE=WORK,VOICE:" + vcard.getTelephoneNumbers().getText() + "\r\n");
contactsList.append("EMAIL;TYPE=PREF,INTERNET:" + vcard.getEmails().getValue() + "\r\n");
String ctype=photoList.get(0).getContentType().toString();
byte[] data = photoList.get(0).getData();
String strdata = new String(data,"UTF-8");
contactsList.append("PHOTO;ENCODING=B;TYPE=JPEG:" + vcard.getPhotos().get(0) +"\r\n");
contactsList.append("END:VCARD\r\n");
fileOutputStream.write(contactsList.toString().getBytes());
}
fileOutputStream.close();
File contactFile = new File(FILES_PATH);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.setDataAndType(Uri.fromFile(contactFile), "text/x-vcard");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch (IOException e1) {
e1.printStackTrace();
}
}
即使我尝试在Android 4.4.4设备中手动导入,它也会在没有图像的情况下导入,但Android 5.0可以正常工作。