4

我正在使用显示所有联系人列表的代码。当我从列表中选择一个联系人时,联系人的详细信息会显示并保存在 .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,无效行:”)任何人都可以帮我解决问题!

4

3 回答 3

3

尝试将ENCODING参数的值从更改BASE64BB是在 3.0 vCards 中使用的正确值。

此外,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();
于 2013-04-01T13:32:34.787 回答
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", "");
}
于 2017-04-12T05:34:59.167 回答
0

这是我为将照片作为 vCard 的一部分发送的操作...需要考虑的重要事项:

1)设备:运行Android 6.01的中兴Axon 7

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;
}
于 2017-12-01T04:14:23.267 回答