【问题标题】:Writing a contact's photo into a vcf file, android将联系人的照片写入vcf文件,android
【发布时间】:2013-03-22 05:50:11
【问题描述】:

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

【问题讨论】:

    标签: java android android-contacts vcf-vcard


    【解决方案1】:

    尝试将ENCODING 参数的值从BASE64 更改为BB 是在 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();
    

    【讨论】:

    • 我也可以在安卓上使用 ez-vcard 库吗?它给了我异常(java.lang.NoClassDefFoundError:ezvcard.VCard)。我在项目中使用了ez-vcard-0.7.3.jar。另外,你能告诉我什么是'B'吗?因为,你告诉我使用 B 而不是 Base64!
    • 我不确定您是否可以将 ez-vcard 用于 android。我认为你可以,因为 ez-vcard 是一个 Java 库。 BBASE64 具有相同的含义。他们只是出于某种原因决定在 3.0 版中使用B
    【解决方案2】:

    这是一篇很晚的帖子,但如果不使用该库,我在 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", "");
    }
    

    【讨论】:

      【解决方案3】:

      这是我为获取一张照片作为 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;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-21
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 2017-07-10
        • 2013-10-03
        • 2014-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多