Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Trouble with loading an image from PCL Storage

Short: I'm trying to load a PNG image from PCLStorage to display to the user on both iOS and Android. I'm currently able to save the image, but having trouble loading it back up because the file cannot be found.

Longer: My goal is to save a webview to view when offline. I tried the PDF route but cannot make that work. I found a way to turn the webview into a PNG using Forms9Patch by converting the HTML using Forms9Patch.HtmlStringExtensions. This saves successfully, but I'm unable to load it again to view it as an image. I'm running into a lot of issues because we are using Xamarin Forms 3.6.0 and are currently unable to upgrade it. Many of the solutions I've found online use packages, such as Evo or a higher level F9P that can't be upgraded due to all the dependencies I have installed. Honestly, I don't care HOW this is done, I just need some way to view the webview in case the user is offline. PDF/PNG/any other method that can work with using older Xamarin Forms. The Forms9Patch method can be found here, http://buildcalc.com/forms9patch/index.html.

Here is what I got so far.

public void saveWebLicense()
{
      var htmlLicenseSource = LicenseSource;
      var fileName = $"{CurrentLicense.customerId}WebViewPNG";
      Forms9Patch.HtmlStringExtensions.ToPng(htmlLicenseSource.Html, fileName, (string path) =>
      {
            if (path != null)
            {
                System.Diagnostics.Debug.WriteLine("A PNG of your HTML is here: " + path);
                CurrentLicense.savedWebViewLicensePath = path;
            }
      });

}

When calling this method I get this returned

A PNG of your HTML is here: /Users/firstname.lastname/Library/Developer/CoreSimulator/Devices/8C7D30F5-F30E-48F6-9584-357193BD5CBB/data/Containers/Data/Application/62DA27D5-6370-4C2A-9B21-8551BEE1611D/tmp/2123927WebViewPNG.png

Then later, attempting to load it I do this.

string loadFileName = $"{license.customerId}WebViewPNG.png";
private async void LoadSavedImage(string filename)
{
      try
      {
            var f = await FileSystem.Current.LocalStorage.GetFileAsync(filename);
            Stream s = await f.OpenAsync(FileAccess.Read);
            SavedLicensePNG.Source = ImageSource.FromStream(() => s);
      }
      catch (Exception ex)
      {
            System.Diagnostics.Debug.WriteLine(ex);
      }
}

With the exception being caught as

PCLStorage.Exceptions.FileNotFoundException: File does not exist: /Users/firstname.lastname/Library/Developer/CoreSimulator/Devices/8C7D30F5-F30E-48F6-9584-357193BD5CBB/data/Containers/Data/Application/62DA27D5-6370-4C2A-9B21-8551BEE1611D/Documents/../Library/2123927WebViewPNG.png

So you can see the path isn't exactly the same. I can open File Explorer locally and verify that the PNG does in fact, exist. I'm just having trouble loading up the image from PCLStorage since I can't find the exact file path.

I would appreciate any help with this!

Currently using:
Xamarin.Forms (3.6.0.709228)
Forms9Patch(1.3.3)

Sign In or Register to comment.