Android vcard 联系人字符串

发布于 2024-10-10 01:54:54 字数 174 浏览 3 评论 0原文

我想知道是否有一种干净的方法可以将电子名片导入为 Android 联系人。我有一个 vcard 解析器,但是映射每个可能的 vcard 字段和字段类型将是一项痛苦且容易出现错误的练习。有更好的办法吗?

Android 联系人看起来很像电子名片,所以我怀疑他们在内部使用电子名片。但是没有公共 API 可以实现这一点。

i'm wondering if there's a clean way to import a vcard as an android contact. i have a vcard parser, but mapping each possible vcard field and field type is going to be a painful, bug-prone exercise. is there a better way?

an android contact looks suspiciously like a vcard, so i suspect they use vcards internally. there's no public API for this however.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-10-17 01:54:54

Android 联系人看起来很像电子名片,所以我怀疑他们在内部使用电子名片。

我认为这比使用“内部 vCard”要复杂得多,特别是因为 vCard 实际上只是一种文本文件格式,因此存储效率低下,搜索大量 vCard 的效率更高。

ContactsContract 的概述(请注意,我在提到“三层数据模型')...

概述

ContactsContract 定义了联系人相关信息的可扩展数据库。联系信息存储在三层数据模型中:

据我所知,有一种方法可以“提取”与单个联系人相关的 vCard,但我不知道有什么方法可以从 vCard 创建联系人。

编辑: WRT 在点击浏览器中的链接时导入 vCard - 是的,好点。我可以确认它至少在通过蓝牙接收 vCard 时可以工作(当然,这是非常合乎逻辑的)。如果我在手机上“打开”vCard,则会出现一个“选择器”,询问我是否要使用“文件编辑器”或“联系人”(HTC 联系人应用程序 - 不确定在其他手机上是否有相同的名称)。如果只有一张 vCard,则无需进一步提示即可导入。如果有多个,我会收到另一个选择器询问我是否要导入一个/多个/全部(多个会提供另一个带有复选框的选择器来进行选择)。

从理论上讲,我认为,可能有可能将一整堆 .vcf 文件转储到某个目录中,并编写一些代码,这些代码只是创建一个 Intent 来“打开”其中一个文件,并将其与 startActivity() 一起使用,而无需指定哪个活动使用。我不确定这里将使用哪个意图操作,但我会从 ACTION_VIEW 开始 - 其他选择可能是 ACTION_EDIT 或(微弱的)ACTION_RUN。

虽然这可能在已知的环境中工作,尤其是一次性的,但它有点混乱,并且行为/结果可能会因不同的手机、Android 版本和联系人应用程序而异。

EDIT2: WRT 使用 Intent 处理 .vcf 文件,我只从我的 SD 卡上尝试过 - 我不知道如何在不先保存的情况下做到这一点。以下内容准确地再现了我打开通过蓝牙发送的 vCard 时看到的内容...

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file:///mnt/sdcard/downloads/bluetooth/MickeyMouse.vcf"), "text/x-vcard");
startActivity(i);

an android contact looks suspiciously like a vcard, so i suspect they use vcards internally.

I think it's a lot more complicated than using 'vCards internally' especially as vCard is effectively just a text file format making it inefficient to store and more so to search a large number of them.

The Overview of ContactsContract (note I cut it at the point it mentions 'three-tier data model')...

Overview

ContactsContract defines an extensible database of contact-related information. Contact information is stored in a three-tier data model:

As far as I can tell there is a way to 'extract' a vCard relating to an individual contact but I don't know of one to create a contact from a vCard.

EDIT: WRT importing vCards when clicking a link in a browser - yes, good point. I can confirm that it works when receiving a vCard by Bluetooth at least (which is, of course, quite logical). If I 'open' the vCard on my phone, I get a 'chooser' asking me if I want to use 'File Editor' or 'People' (the HTC contacts app - not sure if it's called the same on other phones). If there is only one vCard then it imports without further prompting. If there are more than one, I get another chooser asking if I want to import One/Multiple/All (Multiple gives another chooser with checkboxes to make my selection).

In theory I suppose, it might be possible to have a whole load of .vcf files dumped in a directory somewhere and write some code which simply creates an Intent to 'open' one of them and use that with startActivity() without specifying which activity to use. I'm not sure which intent action would be being used here but I would start with ACTION_VIEW - other choices might be ACTION_EDIT or (tenuously) ACTION_RUN.

Although this may work in a known environment particularly as a one off, it is a bit messy and behaviour/results may vary with different phones, versions of Android and contacts apps.

EDIT2: WRT to using an Intent to process .vcf files, I've only tried it from my SD card - I have no idea how to do it without saving it first. The following reproduces exactly what I see when I open a vCard that's sent via Bluetooth...

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file:///mnt/sdcard/downloads/bluetooth/MickeyMouse.vcf"), "text/x-vcard");
startActivity(i);
白色秋天 2024-10-17 01:54:54

我在 Android 中将联系人作为 vCard 导入时遇到了很多困难,花了很多时间后我发现没有用于导入联系人的公共 API。

据我所知,唯一的方法是重用Android的源代码。我通过重用 Android 2.1 源代码实现了相同的目标。

引用

这些文件可以从http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/pim/vcard/exception/ VCardException.java/

在右侧资源管理器中,您将在 pim 文件夹下找到所需的所有文件。负责启动程序的文件是 ImportVCardActivity.java

我希望它有帮助!

I struggled a lot for importing contacts as a vCard in Android and after spending a lot of time I came to there are no public API's for importing contacts.

Only way in my knowledge is to reuse the source code of the Android. I achieved the same by reusing the Android 2.1 source code.

The files can be referred from

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/pim/vcard/exception/VCardException.java/

At right hand side explorer you will find all the files needed under the pim folder. The file which will be responsible to start the procedure is ImportVCardActivity.java

I hope it helps!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文