Android : How to create VCF file from sim contacts in android

on Monday, August 11, 2014


I did following code for creating VCF file for selected contacts for sim contacts.



public void getVCF(ArrayList<String> ids, String path)
{

String[] strids = new String[ids.size()];
strids = ids.toArray(strids);

String cpath = path;

for (int j = 0; j < strids.length; j++) {
Cursor phones = getActivity().getContentResolver().query(
Uri.parse("content://icc/adn"), null,
ContactsContract.CommonDataKinds.Phone._ID+"=?", new String[]{strids[j]}, null);

phones.moveToFirst();
for (int i = 0; i < phones.getCount(); i++) {
String lookupKey = phones.getString(phones
.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);

AssetFileDescriptor fd;
try {
fd = getActivity().getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String VCard = new String(buf);

FileOutputStream mFileOutputStream = new FileOutputStream(
cpath, true);
mFileOutputStream.write(VCard.toString().getBytes());
phones.moveToNext();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}
}


but its not working. it throws below error:



08-12 12:01:26.866: E/AndroidRuntime(29797): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

0 comments:

Post a Comment