Dotnet Maui Blazor App – 如何从 Android 平台的静态类中访问 wwwroot 中的图像?

huangapple go评论117阅读模式
英文:

Dotnet Maui Blazor App - How to access images in wwwroot from static classin android platform?

问题

在Maui Blazor Android平台应用程序中,我需要访问wwwroot文件夹或存储图像文件的某个位置作为文件。

在Windows上:

string rootpath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");

var logoPath = "/img/proxiwash_black.jpg";

运行良好。

当我切换到Android时,它会出现指定文件未找到的错误。

请帮忙。谢谢...

英文:

In Maui Blazor Android Platform App I need to access wwwroot folder or somewhere image files are stored as file.

On Windows :

string rootpath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");

and
var logoPath = "/img/proxiwash_black.jpg";
works well.

When I switch to Android it gives error like specified file not found.

Please help. Thanks...

答案1

得分: 0

对于 Android 平台,静态文件将被打包到 APK 文件中,不会存储在用户可见的任何文件夹中,因此会引发文件未找到的错误。

此外,该文件只能在 Android 上读取。您可以使用 .NET MAUI 文件系统助手 来访问它,例如 FileSystem.OpenAppPackageFileAsync("wwwroot/data.txt")

有关更多信息,您可以参考关于 .NET MAUI 中静态资产仅限于 Razor 组件的官方文档

英文:

For the android platform, the static file will be packaged into the apk file. It will not be stored in any folder which user can see. So it will throw error about file not found.

In addition, the file can only read on the android. And you can use the .net maui file system helpers to access it. Scuh as FileSystem.OpenAppPackageFileAsync("wwwroot/data.txt");

For more information, you can refer to the official document about the Static assets limited to Razor components in the .net maui.

答案2

得分: 0

After getting help with Liyun, I added more and converted stream to byte[] with example:

public static class ResourceLoader {
public async static Task<byte[]> LoadMauiAsset(string fileName)
{
   using var stream = await FileSystem.OpenAppPackageFileAsync(fileName);
            return await StreamToByteArrayAsync(stream);
}

        public static async Task<byte[]> StreamToByteArrayAsync(Stream stream)
        {
            using MemoryStream memoryStream = new MemoryStream();
            await stream.CopyToAsync(memoryStream);
            return memoryStream.ToArray();
        }

    }

I hope this helps everyone.
ps. Do not forget to place your images in "Resources\Raw" in dotnet maui project.

英文:

After getting help with Liyun, I added more and converted stream to byte[] with example:

public static class ResourceLoader {
public async static Task&lt;byte[]&gt; LoadMauiAsset(string fileName)
{
   using var stream = await FileSystem.OpenAppPackageFileAsync(fileName);
            return await StreamToByteArrayAsync(stream);
}

        public static async Task&lt;byte[]&gt; StreamToByteArrayAsync(Stream stream)
        {
            using MemoryStream memoryStream = new MemoryStream();
            await stream.CopyToAsync(memoryStream);
            return memoryStream.ToArray();
        }

    }

I hope this helps everyone.
ps. Do not forget to place your images in "Resources\Raw&quot; in dotnet maui project.

huangapple
  • 本文由 发表于 2023年6月12日 16:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定