Getting image for contacts using Xamarin Forms

alekssmykalekssmyk USMember ✭✭✭

Hi!
I want to fill my listview with contacts list. I use https://components.xamarin.com/view/xamarin.mobile plugin and I can get firstname/ lastname. But I cant get photo stored fot contact. Do you know some other ways for getting contact`s image?

Best Answer

Answers

  • keerthi17keerthi17 INMember

    @deandean I also trying to get image for the contacts from phone contacts. Can you kindly tell me how will you achieve this.?? Thanks a lot. others help also appreciated. Thank you.

  • SamNickSamNick USMember

    @keerthi17 Did you manage to solve this??

  • keerthi17keerthi17 INMember

    @SamNick Hii, Yes Sam I managed to get the contact image by getting the path of the image.. But I did in native(xamarin.ios) only. I attached my code here,

    public List<ContactItem> GetAllContacts()
            {
                var contacts = new List<ContactItem>();
                if (!book.RequestPermission().Result)
                {
                    Console.WriteLine("Permission denied by user or manifest");
                    return null;
                }
                else
                {
                    foreach (Xamarin.Contacts.Contact contact in book)
                    {
                        try
                        {
                            var image = contact.GetThumbnail();
                            var filePath = GetPath(image, contact);
                            Console.WriteLine(filePath);
                            contacts.Add(new ContactItem { ContactName = contact.FirstName, ContactNumber = contact.Phones.First().Number, ContactEmail = contact.Emails.Any() ? contact.Emails.First().Address : null, ContactImage = filePath });
                            Console.WriteLine("{0} {1} {2}", contact.FirstName, contact.LastName, contact.Phones.First().Number);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                    return contacts;
                }
            }
    
            string GetPath(UIImage image, Contact contact)
            {
                var filePathDir = string.Empty;
                var filePath = string.Empty;
                if (image != null)
                {
                    filePathDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                    filePath = System.IO.Path.Combine(filePathDir, contact.Id);
                    if (!System.IO.File.Exists(filePath))
                    {
                        image.AsPNG().Save(filePath, true);
                    }
                }
                return filePath;
            }
    

    Hope it Helps you.

  • SamNickSamNick USMember

    Thanks @keerthi17

  • Richy_GeorgeRichy_George INMember ✭✭✭

    Thanks @keerthi17

  • MarioFrancisMarioFrancis USMember

    i'm having error using xamarin.mobile in my windows phone 8.1 project.. plz any help??

  • sid12sid12 INMember ✭✭

    How can I convert this bitmap image to Xamarin.Forms.Image?
    Please Help Any one
    Thanks

Sign In or Register to comment.