3

I am trying to load the contact images from a cursor, so I have the URI of each image.

But I would like to use the FFImageLoading library to add these images to the view, so that I can easily load placeholders and do a circle transform.

However, I am having difficulty using the library with a URI - I have tried converting the URI to a url using the Path to use the LoadFromURL method, but that has been unsuccessful.

So I am wondering if it would be better to use the LoadImage or LoadStream methods, but I am unsure how best to do so.

Here is what I essentially want to do

    // use FFImageLoading library to asynchronously:
    await ImageService
        .Instance
        .LoadUrl(item.PhotoURL, TimeSpan.FromHours(Settings.ImageCacheDurationHours))  // get the image from a URL
        .LoadingPlaceholder("placeholderProfileImage.png")                                          // specify a placeholder image
        .Transform(new CircleTransformation())                                                      // transform the image to a circle
        .Error(e => System.Diagnostics.Debug.WriteLine(e.Message))
        .IntoAsync(viewHolder.ProfilePhotoImageView);

However, for the images I get from the contacts, I have a Uri and I can load it using the following, but I cant perform a transformation on it:

    var contactUri = ContentUris.WithAppendedId(ContactsContract.Contacts.ContentUri, Contacts[position].LongId);
    var contactPhotoUri = Android.Net.Uri.WithAppendedPath(contactUri, Android.Provider.Contacts.Photos.ContentDirectory);
    viewHolder.ProfilePhotoImageView.SetImageURI(contactPhotoUri);

Also, for relevance here is how I get the contacts:

    var uri = ContactsContract.Contacts.ContentUri;

    string[] projection = {
        ContactsContract.Contacts.InterfaceConsts.Id,
        ContactsContract.Contacts.InterfaceConsts.DisplayName,
        ContactsContract.Contacts.InterfaceConsts.PhotoId
    };

    // CursorLoader 
    var loader = new CursorLoader(activity, uri, projection, null, null, null);
    var cursor = (ICursor)loader.LoadInBackground();

    if (cursor.MoveToFirst())
    {
        do
        {
            Contacts.Add(new Contact
            {

                LongId = cursor.GetLong(cursor.GetColumnIndex(projection[0])),
                LastName = cursor.GetString(cursor.GetColumnIndex(projection[1])),
                PhotoUrl = cursor.GetString(cursor.GetColumnIndex(projection[2]))
            });
        } while (cursor.MoveToNext());
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Pectus Excavatum
  • 3,593
  • 16
  • 47
  • 68
  • 1
    FFImageLoading doesn't support `content://somecontent` urls. You need to get a stream of contact images, eg using helper method like this: http://stackoverflow.com/questions/16295887/get-file-system-path-for-the-contact-photo-in-android Do not forget to add proper `CacheKey("contact id or something""` as `LoadStream` doesn't use cache keys by default. – Daniel Luberda Nov 22 '16 at 18:09

0 Answers0