英文:
Image from AppDataDirectory failed to be displayed in the Image view
问题
当我的 .net maui 应用程序启动时,它会从云端加载一些图像并将它们存储到 FileSystem.AppDataDirectory
文件夹中。然后,在离线模式下,它应该从文件夹中获取它们以在图像视图中显示,但代码不起作用(绑定将被使用,但为了演示,它是硬编码的):
<Image WidthRequest="30" HeightRequest="30">
<Image.Source>
<FileImageSource File="/data/user/0/mytestapp/files/Icontest.svg" />
</Image.Source>
</Image>
如果我将文件来源替换为 URL 来源,相同的图像可以从云端加载。
/data/user/0/mytestapp/files/
是 FileSystem.AppDataDirectory
调用返回的内容,我只是将文件名添加到末尾。
是否支持从文件夹加载图像文件,有没有任何想法?
注意:使用 URL 来源进行缓存不符合应用程序的要求。
英文:
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.
答案1
得分: 1
SVG 预渲染并随您的应用一起传输。您必须在 XAML 中将它们引用为 PNG。(在 iOS 上,如果使用 .svg,甚至不会显示)但问题不仅仅如此。
一旦用 PNG 替换您的 SVG 资源,您将遇到另一个问题。
Android 将缓存您的文件在您的文件系统中,您无法阻止这个行为:https://github.com/dotnet/maui/issues/9773
有两种解决方法:
- 每次更改文件的名称,以便永远不会找到它。
- 通过删除以下位置的所有内容来清除缓存:
Path.Combine(FileSystem.CacheDirectory, "image_manager_disk_cache");
我认为这两种解决方法的负面影响是显而易见的。希望明年能修复这个问题。
英文:
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.
答案2
得分: 1
起初,图像控件无法直接从设备的外部存储获取文件。使用FileImageSource
来获取在项目中设置为嵌入资源的图像文件。
因此,您需要使用流来读取文件,然后使用ImageSource.FromStream
来设置图像源。
例如:
var memoryStream = new MemoryStream();
using (var source = System.IO.File.OpenRead(path))
{
source.CopyTo(memoryStream);
}
image.Source = ImageSource.FromStream(() => memoryStream)
但存在一个关于在将Image.Source
设置为外部存储中的文件时,ImageSource
不起作用的错误。即使图像文件已经作为流读取,图像也不会显示。您可以在GitHub上关注此问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论