我正在使用一个代码,其中显示了所有联系人的列表。当我从列表中选择联系人时,会显示联系人的详细信息并以.vcf文件(以适当的vcard格式)保存,工作正常。当我选择一个有照片的联系人时,它会在imageView中显示照片,但我不知道如何在vcf文件中写入照片。 我使用过这些行,
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,Integer.parseInt(item));
Bitmap photoBitmap;
ContentResolver cr = getContentResolver();
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, photoUri);
photoBitmap = BitmapFactory.decodeStream(is);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photoBitmap.compress(CompressFormat.JPEG, 100 , bos);
byte[] bitmapdata = bos.toByteArray();
imageEncoded = Base64.encodeToString(bitmapdata,Base64.DEFAULT);
String content = "BEGIN:VCARD\nVERSION:3.0\nCLASS:PUBLIC\nPRODID:-//class_vcard from TroyWolf.com//NONSGML Version 1//EN\nFN:"+contactName+"\nTEL;TYPE=cell,voice:"+number+"\nPHOTO;TYPE=JPEG;ENCODING=BASE64:"+imageEncoded+"\nTZ:+0000\nEND:VCARD";
但是我在阅读联系人时遇到错误(“由于意外原因无法解析vCard,无效行:”) 任何人都可以帮我解决问题!
答案 0 :(得分:3)
尝试将ENCODING
参数的值从BASE64
更改为B
。 B
是在3.0 vCard中使用的正确值。
此外,vCard的正确换行符序列为\r\n
,而不是\n
。
您可能有兴趣使用vCard库生成vCard。 ez-vcard就是这样一个图书馆(免责声明:我是作者)。
VCard vcard = new VCard();
vcard.setClassification("PUBLIC");
vcard.setProdId("-//class_vcard from TroyWolf.com//NONSGML Version 1//EN");
vcard.setFormattedName(contactName);
TelephoneType tel = vcard.addTelephoneNumber(number);
tel.addType(TelephoneTypeParameter.CELL);
tel.addType(TelephoneTypeParameter.VOICE);
PhotoType photo = new PhotoType(bitmapdata, ImageTypeParameter.JPEG);
vcard.addPhoto(photo);
vcard.setTimezone(new TimezoneType(0, 0));
String content = Ezvcard.write(vcard).version(VCardVersion.V3_0).prodId(false).go();
答案 1 :(得分:0)
这是一个非常晚的帖子,但我没有使用该库就无法在StackOverflow上找到任何可行的解决方案,所以我认为分享我的发现会很好。
更改编码参数和更正换行序列并不足以构建带有照片的.vcf文件。我还必须在编码到base64之后删除换行符。
将Uri转换为base64格式的示例代码(根据需要替换imageStream初始化)。
// string you can use to write to vcf file (3.0 vCards)
String.format("PHOTO;ENCODING=B;TYPE=JPEG: ,%s\r\n", convertUriToBase64(context, photoUri));
private String convertUriToBase64(Context context, String photoUri) {
InputStream imageStream = null;
try {
imageStream = context.getContentResolver().openInputStream(Uri.parse(photoUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapData = bos.toByteArray();
// line break has to be removed, so it is on the same line as PHOTO
return Base64.encodeToString(bitmapData, Base64.DEFAULT).replaceAll("\n", "");
}
答案 2 :(得分:0)
以下是我将照片作为vCard的一部分发送的内容...... 需要考虑的重要事项:
1)设备:中兴Axon 7运行Android 6.01
2)无法使vCard 3.0或4.0正常运行,只能使用vCard 2.1
File vcfFile = new File(DisplayContactActivity.this.getExternalFilesDir(null), "generated.vcf");
try
{
/**Only Version 2.1 worked for me with or without PHOTO**/
FileWriter fw = new FileWriter(vcfFile);
fw.write("BEGIN:VCARD\r\n");
fw.write("VERSION:2.1\r\n");
fw.write("FN:" + "Contact 7" + "\r\n");
/*Getting the name of the File as I had saved it*/
String file_name = ("current_contact_image" + CONTACT_ID);
/*Get the bitmap that we stored in a File*/
Bitmap bitmap = getContactImage(file_name);
/*Convert bitmap to Base64*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String image_encoded = Base64.encodeToString(b, Base64.DEFAULT);
/*Write the encoded version of image to vCard 2.1, NOTICE that no determining whether the image is GIF or JPEG is needed*/
fw.write("PHOTO;ENCODING=BASE64:" + image_encoded + "\r\n");
/*Write some other stuff to the vCard also just trying to give whoever needs this a starting point*/
fw.write("TEL;Primary:" + "(586) 268-3437" + "\r\n");
fw.write("TEL;OTHER:" + "(313) 313-4545" + "\r\n");
fw.write("ADR;OTHER:" + "12345 AnyLane Dr." + "\r\n");
fw.write("ADR;OTHER:" + "54321 AnyPlace Av." + "\r\n");
fw.write("EMAIL;OTHER:" + "email@yahoo.com" + "\r\n");
fw.write("EMAIL;OTHER:" + "email@wowway.com" + "\r\n");
fw.write("END:VCARD\r\n");
fw.close();
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard");
startActivity(i);
}
catch (Exception e)
{
Log.i(TAG, "Exception: " + e);
}
public Bitmap getContactImage(String file_name)
{
Log.i(TAG, "Running getContactImage() with file name: " + file_name);
Bitmap thumbnail = null;
//------------------------------------------------------------------------------------------
try
{
File filePath = getBaseContext().getFileStreamPath(file_name);
FileInputStream fis = new FileInputStream(filePath);
thumbnail = BitmapFactory.decodeStream(fis);
}
catch (Exception e) //Use scaled_bitmap_for_storage instead of the current_contact_image file name
{
/*Error getting user-selected image, set boolean to get default image*/
Log.i(TAG, "Exception while running getContactImage() for file name: " + file_name + " with error message: " + e);
}
//------------------------------------------------------------------------------------------
return thumbnail;
}