Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android contact photo: How to get back (Google) contact picture from the web (and so, 'cancel' a former local picture change)

Am I the only one trying to achieve this ... :/ ?


In short:

I want to fetch back the picture of my contacts as they do defined it by themselves (on their Google own Account page for instance).

Use case: I have modified one of my contact's picture myself, and now, I want to undo that change -> I want to 'fetch back' the Google picture of my contact (the one set by him/herself).


I have an app that manage Google Contacts. It also manage contact photo using

ContactsContract.CommonDataKinds.Photo.PHOTO

And it's working fine.

Here is a scenario I would like to support:

  1. I add a new contact in my contact list entering it's gmail address. (OK)
  2. After a while, contact photo is available on my contact app (since contact has a picture on its Google account, AND contact sync is ON on the Android device). (OK)
  3. Within my app, I change the app contact picture (so, I 'override' contact picture) (OK)
  4. Within my app, I want to get back default Google contact picture: Not OK. How can I achieve that?

Please take a look at my code here to set the Photo. Should I just 'clear' the photo and relies on ContactProvider to download back user photo from Google account?

How can I clear the photo. Set ContactsContract.CommonDataKinds.Photo.PHOTO to 'null'? and delete the associated file, i.e.,

Uri rawContactPhotoUri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), RawContacts.DisplayPhoto.CONTENT_DIRECTORY)

Thanks for helping.

Here is how I update picture:

    private void updatePhotoThumbnail(Bitmap bitmap, Contact contact) throws Exception
    {
        byte[] contactPhotoBytes = getContactPhotoBytes(bitmap);

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        // @formatter:off
        String where =     ContactsContract.RawContacts.ACCOUNT_NAME + "= ? "
                + "AND " + ContactsContract.RawContacts.ACCOUNT_TYPE + "= ? "
                + "AND " + ContactsContract.Data.CONTACT_ID + "= ? "
                + "AND " + ContactsContract.Data.RAW_CONTACT_ID + "= ? "
                + "AND " + ContactsContract.Data.MIMETYPE + " = ?";
        // @formatter:on

        String[] params = new String[]
        {
                // @formatter:off
                _accountName,
                AccountManagerHelper.GOOGLE_ACCOUNT_TYPE,
                String.valueOf(contact.getId()),
                String.valueOf(contact.getRawContactId()),
                ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                // @formatter:on
        };

        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(where, params)
                .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, contactPhotoBytes).build());

        try
        {
            _contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
            updateDisplayPhoto(contact.getRawContactId(), contactPhotoBytes);
        }
        catch (RemoteException e)
        {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
        catch (OperationApplicationException e)
        {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }


    private void updateDisplayPhoto(long rawContactId, byte[] photo)
    {
        Uri rawContactPhotoUri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
                RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
        try
        {
            AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor(rawContactPhotoUri, "rw");
            OutputStream os = fd.createOutputStream();
            os.write(photo);
            os.close();
            fd.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
like image 583
Pascal Avatar asked Jan 28 '14 15:01

Pascal


1 Answers

Here is a tutorial to retrieve user's Google profile picture. It also retrieves some other things like account Email, name, ... I think this is the straight way for your question :)
Stay in contact if it hepled.

like image 66
Namikaze Minato Avatar answered Nov 15 '22 23:11

Namikaze Minato