Hi.
I'm trying to use the Xamarin.Mobile.Contacts to access the information and then display it in a cross platform listview.
I have problems because Xamarin.Mobile,Contacts.Contact only has the method GetThumbnail() to return the contact image and this method return the type according to the type on dependency used. For example, in android it will return a Android.graphics.Bitmap.
I was trying to get the URI of the bitmap.
In my model I have
public class Contacts
{
#region Properties
public string FirstName { get; set; }
public string LastName { get; set; }
public string Details { get; set; }
public string Thumbnail { get; set; }
}
Then for each platform I was creating a get contacts method that populate a list with my contacts information.
public async Task<List> GetContacts()
{
var contacts = new List();
var permissionResult = await this.book.RequestPermission();
if (permissionResult)
{
if (!this.book.Any())
{
Console.WriteLine("No contacts found");
}
foreach (Contact contact in book.OrderBy(c => c.LastName))
{
var image = contact.GetThumbnail();
byte[] arr = new byte[image.Width * image.Height * 4];
ImageSource retSource = null;
if (image != null)
{
MemoryStream stream = new MemoryStream(arr);
image.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 80, stream);
stream.Flush();
retSource = ImageSource.FromStream(() => new MemoryStream(arr));
}
else
retSource = ImageSource.FromResource("ServiPhone.Images.contacts_gray.png");
contacts.Add(new Contacts() { FirstName = contact.FirstName, LastName = contact.LastName, Details = contact.DisplayName, Thumbnail = retSource});
}
}
return contacts;
}
I load the bitmap and convert it to byte[]. Then, I try to the ImageSource from that byte[]. However, I don't know how to get the URI from the bitmap. Or there is a better way to manage the model more generic using Xamarin.Forms.Image.
Could anyone someone give suggest a better approach?