2

I'm using Xamarin Forms and Xamarin Android.

I've this code for reading some files (stored inside an Android Phone)

public ImageSource Photo { get; set; }

public void LoadPhoto()
{
    var t = Android.Net.Uri.Parse("URI_OF_THE_PHOTO");
    var otherStream = Android.App.Application.Context.ContentResolver.OpenInputStream(t);
    Photo = Xamarin.Forms.ImageSource.FromStream(() => otherStream);
}

then, in my XAML Page, inside a ListView's ViewCell i have this Image control:

<Image Source="{Binding Photo}"></Image>

Eveything works perfectly, the images (about 50) are loaded correctly and displayed inside the ListView.

BUT when i scroll the ListView and the images go out of the screen, when i scroll them back, they're EMPTY ! Images are suddendly gone... DISAPPEARED !!!

I've found a couple of StackOverflow's cases similar to this, but i can't get it to work with those solutions, they seem to not apply to my case.

Please, help, i'm stuck with this problem !

Alessandro
  • 131
  • 1
  • 9
  • Try looking at the `CachingStrategy` for the `ListView` – Gerald Versluis May 23 '18 at 08:36
  • unfortunately, I already tried setting it, with all its possible values ( RecycleElement / RetainElement / RecycleElementAndDataTemplate ) but it doesn't solve the problem at all :-( – Alessandro May 23 '18 at 08:47
  • you can try using FFImageLoading plugin. That can help you cache the images – Sayo Babalola May 23 '18 at 11:57
  • Have you also tried setting the `CachingEnabled` property of the image to `true` ? – Sayo Babalola May 23 '18 at 11:59
  • Yes, guys, i tried with CachingEnabled property of the image to true... it doesn't fix the problem. And i also tried with FFImageLoading plugin, but i can't get the plugin to work with these CONTENT PROVIDER'S STREAMS that i'm working with. I'm desperate ! – Alessandro May 23 '18 at 16:07
  • 1
    You can read [this](https://stackoverflow.com/questions/40738115/android-ffimageloading-using-uri-xamarin), so you get the images from uri? And you want to use FFImageLoading to load the image from uri? – Robbit May 24 '18 at 08:38
  • @JoeLv-MSFT your link solved the problem! Thank you so much – Alessandro Jun 06 '18 at 07:02

3 Answers3

1

You can read this link. FFImageLoading will solve your problem.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • @Alessandro, did it? I replaced `Image` with `CachedImage` and images disappear the same way as they did – Spook Sep 20 '18 at 10:33
1

Use bindable stacklayout in xamarin forms 3.5..i had the same issue..solved..

Vipin Krishna
  • 355
  • 1
  • 4
  • 25
0

I have similar problem, and after Google for a while, problem solved.(maybe this answer is too late)

public ImageSource ByteToImageSource(byte[] byteArrayIn)
{
    //this way, image in ListView will disapear on scroll
    //var stream = new MemoryStream(byteArrayIn);
    //var retSource = ImageSource.FromStream(() => stream);

    // this way is fine
    var retSource = ImageSource.FromStream(() => new MemoryStream(byteArrayIn));
    return retSource;
}

so I think in your case, you could do like this:

Photo = Xamarin.Forms.ImageSource.FromStream(() => Android.App.Application.Context.ContentResolver.OpenInputStream(t));
Kevin Yen
  • 78
  • 2
  • 6