dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

FileSystem.OpenAppPackageFileAsync(string) throws System.IO.FileNotFoundException on Android 14 #26167

Open mapsouza opened 4 days ago

mapsouza commented 4 days ago

Description

I used FileSystem.OpenAppPackageFileAsync to get a Stream from a MauiImage. On iOS all works is fine. But in Android throws System.IO.FileNotFoundException

Steps to Reproduce

  1. Create a new .NET Maui project
  2. Put this code in OnCounterClicked event: `

    private async void OnCounterClicked(object sender, EventArgs e)
    {
    
    var stream = await FileSystem.OpenAppPackageFileAsync("dotnet_bot.png");
    
    using var memoryStream = new MemoryStream();
    await stream.CopyToAsync(memoryStream);

    }

`

  1. Debug

Link to public reproduction project repository

https://github.com/mapsouza/FileNotFoundImage

Version with bug

8.0.100 SR10

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

Zhanglirong-Winnie commented 3 days ago

This issue has been verified using Visual Studio 17.13 Preview 1 (9.0.10 & 8.0.100 & 8.0.83). Can repro this issue on Android platform. Image

MarcAlx commented 3 days ago

Same problem observed here, dotnet 9.0.0.

From the documentation

FileSystem.OpenAppPackageFileAsync
Files that were added to the project with the Build Action of MauiAsset can be opened with this method. .NET MAUI projects will process any file in the Resources\Raw folder as a MauiAsset.

There seems to be a tolerance on iOS with MauiImage.

Here's a simple workaround:

In your .csproj after:

<!-- Images -->
<MauiImage Include="Resources\Images\*" />

Add the following line:

<!--to allow FileSystem.OpenAppPackageFileAsync on image-->
<MauiAsset Include="Resources\Images\*" LogicalName="%(RecursiveDir)%(Filename)%(Extension)"/>
mapsouza commented 3 days ago

Same problem observed here, dotnet 9.0.0.

From the documentation

FileSystem.OpenAppPackageFileAsync
Files that were added to the project with the Build Action of MauiAsset can be opened with this method. .NET MAUI projects will process any file in the Resources\Raw folder as a MauiAsset.

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-system-helpers?view=net-maui-9.0&tabs=android#bundled-files

Above, it is documented that it opens any file within the package and in the platform section, it states that it only opens MauiAsset. But this information are in all platforms.

I think this worked in a past because @jfversluis have's a post in your blog and a demo written in .net 7.

https://github.com/jfversluis/MauiBase64ImageSample