Image from AppDataDirectory failed to be displayed in the Image view - xamarin

When my .net maui app starts it loads some images from the cloud and stores to FileSystem.AppDataDirectory folder. Then in offline mode it supposed to take them from the folder to display in the Image view, but the code does not work (binding will be used, but for the demo sake it's hardcoded):
<Image WidthRequest="30" HeightRequest="30">
<Image.Source>
<FileImageSource File="/data/user/0/mytestapp/files/Icontest.svg" />
</Image.Source>
</Image>
Same image works fine if loaded from the cloud when I replace File source to Url source.
/data/user/0/mytestapp/files/ is what FileSystem.AppDataDirectory call returns, I just added file name to the end
Any idea if it's supported at all to load image files from the folder?
note: caching with url source does not serve the app requirements.

SVG get pre-rendered and ship with your app. You have to reference them as PNG in your XAML. (On IOS, it wont even display if you use .svg) But this is not it.
Once replace your SVG resources with PNG. You will hit another problem.
The android will cache your files in your file system, and there is nothing you can do about it: https://github.com/dotnet/maui/issues/9773
There are two work arounds:
Change the name of the file every time, so it is never found.
Clear the cache by deleting everything from here: Path.Combine(FileSystem.CacheDirectory, "image_manager_disk_cache");
I think the negative effects of both workarounds are obvious. Fingers crossed that it gets fixed next year.

At first, the Image control can't get the file from the device's external storage directly. The FileImageSource is used to get the image file in the project which is set as embedded resource.
So you can need to use a stream to read the file and then use the ImageSource.FromStream to set the image source.
Such as:
var memoryStream = new MemoryStream();
using (var source = System.IO.File.OpenRead(path))
{
source.CopyTo(memoryStream);
}
image.Source = ImageSource.FromStream(() => memoryStream)
But there is a bug about ImageSource is not work when set Image.Source to file in external storage. Even the image file has been read as a stream, the image will not display. You can follow up this issue on the github.

Related

How to open image file using pdftron

I want to load an image file using the pdftron webviewer api.
For pdf files it's working fine but I want to load image files and add annotations to them.
In the user guide there is no information regarding working with images.
Yes, WebViewer can do this out of the box.
There are a number of image formats supported. Here is the breakdown.
PNG, JPEG
Simply pass the image URL to WebViewer initialDoc constructor parameter, or to WebViewer.loadDocument().
If your URL does not have a proper file extension, then you can do the following.
myWebViewer.loadDocument("mydomain/generic_url_to_image", {filename: "input.png"})
This works with both PDFNetJS Lean and Full editions.
TIFF, GIF, BMP
You can utilize the browser's HTML5 canvas to load the images, and then generate PNG/JPG from the HTML5 canvas, and then pass the follow the instructions above.
JP2 or multiple images in one document.
Using PDFNetJS Full edition, which does not come with the standard WebViewer download, you have full control over PDF creation.
PDFNetJS Full Download: http://pdftron.com/downloads/PDFNetJS.zip
Using PDFNetJS Full you would follow the AddImage sample code to construct a PDF with the image(s) in it.
http://pdftron.com/webviewer/pdfnetjs/config.html#file=samples/PDFNet/AddImageTest/AddImageTest.js

Reading JPG into BitmapData in ActionScript 3 (JPG is inside APK file)

I'm using Adobe AIR to make APK file for my Android phone. Inside the APK package there is a SWF file and in the same folder as the SWF file there is a subfolder named "images", which contains image.jpg.
I'm trying to read that image.jpg into the program at runtime but can't get the location of it (the program doesn't seem to find it.
Here's what I'm trying:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("images/image.jpg"));
while (bitmapData==null) {} //wait until loaded
return bitmapData;
The onComplete-function puts sets the bitmapData field. However, the COMPLETE event never happens so the while loop never exists.
I have also tried:
loader.load(new URLRequest("/images/image.jpg"));
loader.load(new URLRequest("app:/images/image.jpg"));
loader.load(new URLRequest("app-storage:/images/image.jpg"));
loader.load(new URLRequest("file:/images/image.jpg"));
None of it works.
I can't get this to work even when trying without the APK file, when only running the SWF file that is generated (the images subfolder is in the same folder as the SWF file).
I can't embed the JPG into the SWF file itself because I need to have a lot of image files and Flash CS6 runs out of memory when making such a big SWF file (reading the images externally would also mean a whole lot less compilation time).
Any ideas? How can I get put the external JPG file into BitmapData? Remember that the image is not outside of the APK file, it is packaged inside it.
Note: I'll also export the thing into a iOS ipa package later so it have to work there as well.
Edit because I can't self-answer due to my reputation.
Strille made me realize that the ActionScript Virtual Machine is single-threaded.
So it's impossible to make a thread sleep (wait) for an image being loaded. Instead I have to sigh rewrite a lot of code so that things are halted and continues when the onComplete function is called.
This thing that I tried before actually works but couldn't complete due to the while loop:
loader.load(new URLRequest("app:/images/image.jpg"));
If I remember correctly, you should be able to do it like this:
loader.load(new URLRequest(new File("app:/images/image.jpg").url));
Since we're using the File class, the above will not run in the Flash Player, just on iOS or Android.
From my experience there are two practical methods to load assets from a running Android APP using AIR
using URLLoader and .url property of the File instance
using FileStream (which receives a File instance)
Adding to project
You can package the files inside the APK using
the same technique as packaging ICONS, inside the .xml descriptor file for your app.
And make sure you add those to your IDE in the Android section (applies to IntelliJ and Flash Builder) "Files and folders to package"
- add the path to your wanted assets folder, and give it a relative name alias.
Elaboration
The assets themselves are placed (when the APP is installed),
inside the ''applicationDirectory'' which is available for AIR,
Yet since newer Android OS's, and Flash player security issues,
it is not possible to access those assets via the nativePath,
only the 'url' property of the File instance.
Best Practice
Use URLLoader and .url property of the File instance,
as it is also Asynchronous by nature and can resolve PNG / JPG files
using native decoder instead of having to do this manually
reading a FileStream (which returns a a ByteArray)
var f:File = File.applicationDirectory.resolvePath('images/myimage.png');
loader.load(new URLRequest(f.url));
and,
If I'm missing more ways, please let know... )

Flash pull in images from directory

I'm trying to create a flash image slider however I need the images to be pulled into the flash file from a directory and then to transition fade through to the next ones.
Has anyone got any ideas of where to start on this or any links that could help?
Much Appreciated.
Pull in images from a local or server directory? For a server directory you would use the Flash Loader class Loader
If you are using Adobe AIR then you can load local files using FileReference.
For fading, I use Tweenlite and tween the alpha value of the from image and the to image to make one fade out and the other fade in.
Flash can't read the content of the folder.
You either have to specify the file name(s) manualy either use a server-side script to create a list of the files in the folder - it may be passed to Flash as a XML, text file or just a query string, up to you.

Silverlight: Image Source with no file extension doesn`t display

In my Silverlight project, images for which the source URI does not contain the file extension don`t display, although the documentation says it should.
I set the image source like so:
imgCompanyLogo.Source = new BitmapImage(new Uri(Application.Current.Host.Source, "/Files/" + logoName));
Now, if "logoName" contains the file extension (like ".png" for example), the image is displayed fine, but it simply doesn't if the file is stored without an extension.
This seems to contradict the documentation here which states:
"The format-specific filename extensions such as .png are not necessarily required to be in the URI naming, but if the retrieved file is not determined to be a valid image format, a runtime exception is thrown."
I'm not getting any runtime exception either.
Is this a known issue or am I missing something simple?
Thanks!
PS: Just a little twist, the images display fine while debugging, not when the system is deployed...
I've made some test and the problem seems to be due to the response from the server.
If you try using .png within your project with a Build Action sets to Resource, both images will load regardless the extension.
Now if you try with images hosted on a server, it won't have the same behavior. Actually, if you try to browse the link to an image without extension directly in your browser, it will result in something else. On Chrome it will download the file, and on IE it will display the result as plain text.
That's because of the MIME type. A png should be returned with the type image\png.
There is a trick with .htaccess to set up the MIME-type but you need to have to specify for which extension. It works like this:
AddType image\png yourExtension [Extension2] [Extension3] ..
And if you want to see if why the image didn't load on your Image control, you can add an eventhandler to the ImageFailed event:
<Image Source="..." ImageFailed="Image_ImageFailed" />
But the error message that you will see is not really helpful:
ErrorException = {System.Exception: AG_E_NETWORK_ERROR}

Image not showing IE(all the versions)

i tried all the versions of ie but it is not showing only 1 image.
<asp:Image ID="img" runat="server" ImageUrl="~/images/FlorDecor.jpg" BorderColor="AliceBlue" BorderStyle="None" Height="126px" Width="90px" />
i tried placing this image outside the folder and giving
url = "FlorDecor.jpg"
url = "../FlorDecor.jpg"
url="~/FlorDecor.jpg"
i tried all this stuff pls help me..
Your URL references were not all valid in Silverlight (should have been "images/FlorDecor.jpg" if the file is in the images folder of your Silverlight App).
Silverlight uses different URL methods depending on whether the image is in the Silverlight project or somewhere on the source website (or an external site).
If the problem persists the cause is more likely that your JPG is not compatible for some reason.
Convert it to a PNG with Paint.Net or similar to test that theory, or just grab another JPG from the Internet instead.
Do not use .jpg file, use .png. It will works.

Resources