我正在尝试将 vcard(版本 3.0)自动导入到 android 联系人中。在联系人管理器中有一个选项可以将存储在 sd 卡上的 vcf 文件导入到联系人中。如何通过移交文件来触发此功能?
问问题
5950 次
1 回答
3
我在我的一个项目中使用了以下解决方案。它工作得很好。作为 vCard 库,我使用了 cardme v.0.26,它也支持 vcard 3.0 版。
注意:文件名存储在 res/values/strings.xml...
/*
* Create file. This file has to be readable, otherwise the contact
* application cannot access the file via URI to read the vcard
* string.
*/
// 此 var 包含您的 vCard 作为字符串 String vcardString;
FileOutputStream fOut = openFileOutput(
getResources().getText(R.string.app_vcard_file_name)
.toString(), MODE_WORLD_READABLE);
osw = new OutputStreamWriter(fOut);
// write vcard string to file
osw.write(vcardString);
// ensures that all buffered bytes are written
osw.flush();
} catch (NotFoundException e) {
// ..
} catch (IOException e) {
// ...
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
// ...
}
}
}
// let the os handle the import of the vcard to the Contacts
// application
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://"
+ getFileStreamPath(
getResources().getText(R.string.app_vcard_file_name)
.toString()).getAbsolutePath()), "text/x-vcard");
startActivity(i);
于 2011-08-23T00:39:17.910 回答