首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试将联系人转换为电子名片时的FileNotFoundException

尝试将联系人转换为电子名片时的FileNotFoundException
EN

Stack Overflow用户
提问于 2010-03-31 19:26:24
回答 2查看 1.3K关注 0票数 5

我尝试使用下面的代码将模拟器上的联系人转换为VCard格式。

AssetFileDescriptor afd =openAssetFileDescriptor(Contacts.CONTENT_VCARD_URI,"r")

堆栈跟踪显示content://com.android.contacts/contacts/as_vcard上没有java.io.FileNotFoundException文件

我们是否需要将文件附加到URI?在Android中,有没有其他方法可以将联系人转换为Vcard?

EN

回答 2

Stack Overflow用户

发布于 2010-05-26 10:54:28

您必须遍历联系人数据库,并对每个数据库分别调用openAssetFileDescriptor()。重要的是,您必须为每个联系人使用查找键,并使用URI.withAppendedPath()方法将其附加到CONTENT_VCARD_URI。

票数 2
EN

Stack Overflow用户

发布于 2011-05-20 08:59:54

我只是碰巧碰到了这个。这里有一种方法可以做到。首先,让用户选择他的联系人或通过其他方式获取contactUri。

有了vcard之后,您可以查找lookup_key,然后就可以检索vcard了。这是我在获得contactUri后使用的代码(类似于从不同的函数复制粘贴,但应该可以工作)。

代码语言:javascript
复制
Cursor cursor = resolver.query(contactUri, new String[] {
    Contacts.LOOKUP_KEY
}, null, null, null);
FileInputStream input = null;

try {
    if (cursor.moveToFirst()) {
        return cursor.getString(0);
    } else return;

    AssetFileDescriptor afd = context.getContentResolver().openAssetFileDescriptor(
            Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey), "r");
    input = afd.createInputStream();

    int ch;
    StringBuffer strContent = new StringBuffer("");
    while ((ch = input.read()) != -1)
        strContent.append((char) ch);

    Log.d(TAG, strContent.toString());
} finally {
    cursor.close();
    if (input != null) {
        input.close();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2552431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档