0

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...

2 Answers2

0

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.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
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.