onupdatecascade
onupdatecascade

Reputation: 3366

How can I display images from an arbitrary folder in Xamarin Forms on Windows 8

This may be a dead horse, but I need to be able to supply a local folder full of images OUTSIDE my Xamarin application - not as resources, not requiring compilation to add more images - and display those images in the application, in Image objects. My main target platform is Windows 10. Others nice to have, not required.

Xamarin Forms Image normally takes either a File name (no path) or a URI. I can't get either method to locate and display images from the local file system. I must be doing something basic wrong.

Non-working sample:

Image i = new Image();
i.Source = ImageSource.FromFile(some.png); // Needs to be from folder on local disk
grid.Children.Add(i, c, r);

Most articles I find explain how to bundle images WITH the application as part of the source; again I need to display them in the application from a folder WITHOUT bundling them with the application. Users should be able to add more images to the folder and they would work in the app without recompiling/reinstalling - like an image gallery.

EDIT: I am able to successfully read a text file using PCLStorage https://github.com/dsplaisted/pclstorage. Is there a way to wire that to Xamarin forms image?

Upvotes: 3

Views: 2028

Answers (2)

Jason
Jason

Reputation: 89204

You would need to write a platform specific class to read the images and return a stream that could be consumed by StreamImageSource. Then you can use DependencyService to expose this behavior to your Forms PCL.

Alternatively, you could use PCLStorage

  var f = await FileSystem.Current.LocalStorage.GetFileAsync (path);

  Stream s = await f.OpenAsync (FileAccess.Read);
  image.Source = ImageSource.FromStream(() => s);

Upvotes: 5

Cheesebaron
Cheesebaron

Reputation: 24470

Basically, you can't. If you don't bundle the images with your application, you somehow have to transfer the images to the application. Most common case is that you serve these images on a web server somewhere, where the application downloads the images from that web server.

Upvotes: 0

Related Questions