Import a vcard 3.0 automatically into android contacts (Android 2.1)
I'm trying to import a vcard (vers. 3.0) automatically into the android contacts. Within the contact manager there is an option to import a vcf file stored on the sd-card into the contacts. How c开发者_如何学Can I trigger this function with handing over a file?
I used for one of my projects the following solution. It worked perfectly. As the vCard lib I used cardme v.0.26, which supports vcard version 3.0 as well.
note: file name is stored in 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.
*/
// this var contains your vCard as a string 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);
精彩评论