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

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?

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);

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.

Related

Xamarin Essentials FileSystem can you save async

Looking into Xamarin essentials and wondering what the purpose of FileSystem is.
I was looking for something that would load and save a file for me async out of the box.
It looks to me and may be I am not understanding the usage that the only thing that it gives you is a location where to save and load the file and there is no functionality to actually save it for you eg "Old PCLStorage"
is this correct?
All Xamarin Essentials File System Helpers are doing is providing access to two different directories within your app, CacheDirectory and AppDataDirectory and access to the read-only pre-bundled files within application so you do not have to write platform-based code for these locations and do your own DI (or use Forms' DI) to access them...
Once you have the string-based directory location, then you use the normal .Net(Std) file and directory I/O routines. Create/delete subdirectories, create/read/write/delete a file using the async (or not) functions from the .Net(Std) framework or a third-party framework/package. Your choice...
CacheDirectory
The cache directory is a read/write location and it implements access to the "native" platform cache location. On Android this is the Cache
var cacheDir = FileSystem.CacheDirectory;
AppDataDirectory
The app data directory is the "default" location for where regular files should be stored.
As the docs state:
any files that are not user data file
FYI: This is not the place to be used for Sqlite databases and such if you are implementing/complying with platform dependent norms... Why Essentials did not include a database location is unknown to me when platforms like iOS and Android have APIs for them and formally documented locations... ;-/
Application bundled files (read-only)
OpenAppPackageFileAsync provides access (via a Stream) to read-only bundled files in your application (AndroidAssets, BundleResource, etc..)
using (var stream = await FileSystem.OpenAppPackageFileAsync("sushiLogo.png"))
{
using (var reader = new StreamReader(stream))
{
var fileContents = await reader.ReadToEndAsync();
}
}

loading Image from QUrl with blackberry 10

bb::cascades::Image m_image = Image(QUrl(appRoot +"default.png"));
If the file does not exist what m_image returns ??
It should return null
One of the benefits of packaging an image with the application is that
it allows the tool to verify the images and optimize them for the
devices they are targeting. Because they are packaged with the
application, it should be assumed that they are instantly available,
and should never fail to load. If an incorrect name is provided when
an asset is created, an null image is returned.
Image documentation

Unable to access the image from src/images folder

I have created images folder under src in Netbeans..to get image using following code in my application.
URL url = ClassLoader.getSystemClassLoader().getResource("/images/file.jpg");
logoimg=new ImageIcon(url);
but it is not loading any image..I have tried many of existed solutions in web, but no use.
My requirement is need to run my application jar in any system without missing images.
following code fixed my probelm
ImageIcon ImageIcon = new ImageIcon(getClass().getResource("/images/logo.png"));

Can you use a URL to embed an image inline in MVCMailer

In looking at the MVCMailer step by step, I see that you can embed images inline using cid in the HTML.
https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
I see the code that talks about embedding:
#Html.InlineImage("logo", "Company Logo")
var resources = new Dictionary<string, string>();
resources["logo"] = logoPath;
PopulateBody(mailMessage, "WelcomeMessage", resources);
My question is rather than use a path within the site like:
resources["logo"] = Server.MapPath("~/Content/images/logo.png");
Can I somehow get at images I have in the Azure Cloud or S3 Cloud where I have plenty of space?
Please let me know if there is a way to get the image from some other source than the server the MVC mailer is running on.
Thanks,
Victor
In Server.MapPath(my_path), my_path, specifies the relative or virtual path to map to a physical directory. Server.MapPath is related with local FileSystemObject so in that case it will look for the file (you pass as parameter) in a location within local web server. More info is described here:
http://msdn.microsoft.com/en-us/library/ms524632(v=VS.90).aspx
So if you image is located at http://azure_storage_name.blob.core.windows.net/your_public_container/image_name.jpg, the passing it to Server.MapPath() will not work as this is not a location in local file system. I tried and found that it will not accept and I also try to tweak a few way but no success. IF you can change the code behind and which can access the resources from a URL then it will work otherwise not.

Is it possible to get the picture token from Picture Class?

to receive a Picture over Picture Hub this code works fine:
MediaLibrary library = new MediaLibrary();
Picture picture = library.GetPictureFromToken(NavigationContext.QueryString["token"]);
Is it possible, to get the token string from the Picture instance?
Any casts your tricks?
To uniquely identify a Picture in the MediaLibrary you'l need to use a combination of Name and Date.
You coudl also try using HashCode but when you have a large number of images it can be easier to find an image by name than checking the HashCode of all images.
From what I can gather, the token is provided to your application (intended as an "Extras" application) by the operating system, but there is no way for you to get a token for a specific image. What are you trying to achieve? Are you creating a Silverlight or XNA application?
You may find the following resources useful:
WP7–Adding to the Image Hub 'extras' menu
WP7 Saving to Media Library

Resources