0

how can i get the Contact ID and Contact group? i have used the following code which gives me the name and phone any all other info but not the id or the group

public void ExportContacts() {
            vCard = new ArrayList<String>();
            cursor = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            int ss = cursor.getCount();
            if(cursor!=null&&cursor.getCount()>0)
            {
                cursor.moveToFirst();
                for(int i =0;i<cursor.getCount();i++)
                {

                    get(cursor);
                    Log.d("TAG", "Contact "+(i+1)+"VcF String is"+vCard.get(i));
                    cursor.moveToNext();
                }

            }
            else
            {
                Log.d("TAG", "No Contacts in Your Phone");
            }

        }

        public void get(Cursor cursor)
        {
            //cursor.moveToFirst();
            String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
            AssetFileDescriptor fd;
            try {
                String _id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "_ID");
                FileInputStream fis = fd.createInputStream();
                byte[] buf = new byte[(int) fd.getDeclaredLength()];
                fis.read(buf);
                String vcardstring= new String(buf);
                vCard.add(vcardstring);

                String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
                FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, true);
                mFileOutputStream.write(vcardstring.toString().getBytes());

            } catch (Exception e1) 
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

i want to put the ID and the Group name in the vCard that i write in file. can any one help?

AnasBakez
  • 1,230
  • 4
  • 20
  • 36

1 Answers1

0

Depending on which ID you want, you can use this or this for the ID field.

Group name is potentially somewhat messier: this is the row for group title, which may be what you need, but you probably need to do the appropriate error checking in case there isn't a group defined.

EDIT: Group ID you can find here.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • i have got the ID of the contact like this "String _id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));" but what i want is how to add it to the vCard, iam getting the card from array of bytes – AnasBakez May 20 '12 at 12:30