0

I am creating a hybrid mobile app, using Xamarin.Forms.

I want the user to click a button and the contact in the app gets inserted into the phone's contacts, including the profile picture.

So far, everything on the iOS side is working. I successfully insert the profile picture.

I can't get it to work on Android though. I've already tried looking on here but all solutions use Java. Inserting the name, number and e-mail works perfectly.

Here is my code so far:

  public void SaveContacts(string name, string number, string email, string job, string company, byte[] imageData)
        {
            try
            {
                var intent = new Intent(Intent.ActionInsert);
                intent.SetType(ContactsContract.Contacts.ContentType);
                if (!string.IsNullOrWhiteSpace(name))
                    intent.PutExtra(ContactsContract.Intents.Insert.Name, name);
                if (!string.IsNullOrWhiteSpace(number))
                    intent.PutExtra(ContactsContract.Intents.Insert.Phone, number);
                if (!string.IsNullOrWhiteSpace(email))
                    intent.PutExtra(ContactsContract.Intents.Insert.Email, email);


                if (imageData.Length != 0)
                {
                   // INSERT PROFILE PICTURE HERE
                }

                Platform.CurrentActivity.StartActivity(intent);
            }
            catch (Exception ex)
            {
                DependencyService.Get<ILogger>()?.Error(ex);
            }
        }

How do I add the profile picture to the contact?

Lisa Maria
  • 27
  • 1
  • 5
  • *"all solutions use Java"*: The code above is in your Android project, so has access to Android APIs? Add to question a link to a java solution. Manually translate each line of java code into the corresponding xamarin.android call. Add the result to question. Indicate any lines that you are having trouble writing in c#. – ToolmakerSteve Nov 26 '22 at 02:47

1 Answers1

0

According to this case which is about Insert Contact (ContactsContract) via Intent with Image (Photo), you need to use the intent.putParcelableArrayListExtra(Insert.DATA, data) method to add the photo to the new contact.

But I have create a sample to test it. This method have a bug. I use the following code:

List<ContentValues> data = new List<ContentValues>();
ContentValues row1 = new ContentValues();
Intent intent = new Intent(Intent.ActionInsert);
intent.SetType(ContactsContract.Contacts.Photo);
intent.PutParcelableArrayListExtra(ContactsContract.Intents.Insert.Data, data);

But it will show the type convert error. Even though the class ContentValues is inherited from the IParcelable interface. The error still show. You can report it to the xamarin on the github.

In addition, I also try to use the method about using the content provider to insert a contact. But the RawContacts doesn't have ACCOUNT_TYPE property.

So for the bug, it seems you can't add profile picture to contacts with the native api.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14