【问题标题】:Importing VCard file with ez-vcard library for Android使用适用于 Android 的 ez-vcard 库导入 VCard 文件
【发布时间】:2016-05-22 05:17:11
【问题描述】:

我正在尝试使用 ez-vcard 库和 Android 桥接器(直接从 Github 复制的类)将所有联系人数据导出到 vcf 文件。然后我想从此文件恢复联系人。所以我在恢复之前手动删除了一个电话联系人。但是,似乎已删除的联系人没有恢复。在检查输出 vcf 文件时,我可以看到联系人已正确导出,因此还原过程似乎有问题。

没有错误消息。通过 Gmail 导入相同的文件时,它可以工作。但是我需要在后台线程中静默完成导入过程,没有任何 UI,所以我无法通过联系人应用程序进行导入,这就是我求助于 ez-vcard 库的原因。

https://github.com/mangstadt/ez-vcard

https://github.com/mangstadt/ez-vcard-android

导出(为清楚起见,删除了错误处理):

public void exportContacts(Context context) {
    Cursor c = context.getContentResolver()
            .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (c != null) {
        if (c.moveToFirst()) {
            do {
                writeContactToFile(context, c);
            } while (c.moveToNext());
        }
        c.close();
    }
}

private void writeContactToFile(Context context, Cursor c) {
    String lookupKey = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
    AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(uri, "r");
    FileInputStream fis = fd.createInputStream();
    byte[] buf = new byte[(int) fd.getDeclaredLength()];
    fis.read(buf);
    String vCard = new String(buf);
    FileOutputStream fos = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE | Context.MODE_APPEND);
    fos.write(vCard.toString().getBytes());
}

进口:

VCardReader reader = new VCardReader(file);
reader.registerScribe(new AndroidCustomFieldScribe());
ContactOperations operations = new ContactOperations(context);
VCard vCard;
while ((vCard = reader.readNext()) != null) {
    operations.insertContact(vCard);
}

可能是一些编码问题还是我缺少库的一些预配置?

提前致谢。

【问题讨论】:

    标签: android contacts android-contacts vcf-vcard


    【解决方案1】:

    您确定将正确的File 对象传递给VCardReader 吗?如果在文件中找不到任何 vCard,则永远不会调用 insertContact 方法,因为 reader.readNext() 只会返回 null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多