Hi,

I'm having a problem with the process of adding a photo to a contact.

My flow is like this: i'm getting the _ID from the
ContactsContract.PhoneLookup then i search in ContactsContract.Data
for all the rows that ContactsContract.Data.CONTACT_ID = _ID from
those results i get the RAW_CONTACT_ID and then i insert a new row
with the image details.

        String phoneNum = "555555555";
    Uri uri =
Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNum));

    String[] projection = new String[]{PhoneLookup._ID,
PhoneLookup.PHOTO_ID};
    Cursor cursor = getContentResolver().query(uri, projection, null,
null, null);

    int photoColumn =
cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID);
    int idColumn =
cursor.getColumnIndex(ContactsContract.Contacts._ID);

    if (cursor.moveToFirst())
    {
        if(cursor.getString(photoColumn) == null)
        {
            Log.d("test", "Photo Not Exist for " + phoneNum);

            long id = cursor.getLong(idColumn);
            Cursor dataCursor =
getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                    new String[]{ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME},
                    ContactsContract.Data.CONTACT_ID + "=" + id ,
null, null);

            String[] names = dataCursor.getColumnNames();
            int namec =
dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
            int rowIdCol =
dataCursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
            dataCursor.moveToFirst();

            String n = dataCursor.getString(namec);
            long rawId2 = dataCursor.getLong(rowIdCol);
            Log.d("test", "Contact Id: " + id +"\nName: " + n);

            File img = new
File(Environment.getExternalStorageDirectory(),"contactImage.jpg");
            FileInputStream fis = null;
            try
            {
                fis = new FileInputStream(img);
            } catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Bitmap b = BitmapFactory.decodeStream(fis);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] data = baos.toByteArray();

            ContentValues values = new ContentValues();
            values.put(ContactsContract.Data.RAW_CONTACT_ID, rawId2);
            values.put(ContactsContract.Data.CONTACT_ID, id);
            values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
            values.put(ContactsContract.CommonDataKinds.Photo.PHOTO,
data);
            values.put(ContactsContract.Data.MIMETYPE,
 
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE );
            Uri newRowUri =
getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
values);
        }
        else
        {
            Log.d("test", "Photo Exist for " + phoneNum);

        }
    }

The problem is that sometimes the image is added to the contact but
its not shown in the edit contact screen and sometimes the image is
not added.

What am i doing wrong?

thanks,

Eitan.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to