Get a contact image of Xamarin Forms

I am creating a hybrid mobile application using Xamarin Forms. I want to display a list of all phonebook contacts with the following data:

  • Name
  • Picture

I added the permission "READ_CONTACTS" in the android manifest. Below is the code to get all contacts:

var contactList = new List < MContacts > ();

var ContactDetailURI = ContactsContract.Contacts.ContentUri;

string[] ContactDetailProjection = {
 ContactsContract.Contacts.InterfaceConsts.Id,
 ContactsContract.Contacts.InterfaceConsts.DisplayName,
 ContactsContract.ContactsColumns.ContactLastUpdatedTimestamp,
 ContactsContract.Contacts.InterfaceConsts.PhotoId,
 ContactsContract.Contacts.InterfaceConsts.PhotoUri,
 ContactsContract.Contacts.InterfaceConsts.PhotoFileId,
 ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri
};

var ContactDetailCursor = Forms.Context.ContentResolver.Query(
 ContactDetailURI,
 ContactDetailProjection,
 null,
 null,
 null
);

if (ContactDetailCursor.MoveToFirst()) {
 do {
    var contact = new MContacts();
    contact.Id = ContactDetailCursor.GetLong(ContactDetailCursor.GetColumnIndex(ContactDetailProjection[0]));
    contact.DisplayName = ContactDetailCursor.GetString(ContactDetailCursor.GetColumnIndex(ContactDetailProjection[1]));
    contact.PhotoId = ContactDetailCursor.GetString(ContactDetailCursor.GetColumnIndex(ContactDetailProjection[6]));

    contactList.Add(contact);

 } while (ContactDetailCursor.MoveToNext());
}

return contactList;

I have a XAML page that will display data. I am using Image Cell. The following is the XAML code:

<ContentPage.Content>
    <ListView x:Name="ContactList">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ImageCell
                    Text="{Binding DisplayName}"
                    ImageSource="{Binding PhotoId}">
                </ImageCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

I get the name of the contact, but in the image field I get only this:

content://com.android.contacts/contacts/1/photo

What to do to display the image? What am I missing here?

PS I do not want to use the Xamarin.Mobile component or any other component.

+4
source share
1

Android Content Providers Content Resolvers. URL content:// . :

// convert uri to stream (Android code, ContentResolver is a property of the Activity class):
var stream = ContentResolver.OpenInputStream(uri);

// or when not in an activity (e.g. a service):
var otherStream = Android.App.Application.Context.ContentResolver.OpenInputStream(uri);

// eventually convert the stream to imagesource for consumption in Xamarin Forms:
var imagesource = Xamarin.Forms.ImageSource.FromStream(() => stream);

. :

+3

Source: https://habr.com/ru/post/1676234/


All Articles