Is there anywhere that describes the Android file system with respect to MAUI?

Lloyd Sheen 1,476 Reputation points
2023-04-19T17:32:21.8966667+00:00

As in the question I am trying to learn about files on Maui/Android. I can use VS to add files as MauiAssets and read them. First what I would like to do is when putting those files into the solution have a folder in VS and cause all those files and the containing folder be copied when the app is deployed. I would also like to know how to get a list of the files within a folder in Android.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2023-04-19T20:41:27.9533333+00:00

    you should review the android documentation: https://developer.android.com/training/data-storage

    1 person found this answer helpful.

  2. Rob Caplan - MSFT 5,437 Reputation points Microsoft Employee
    2023-05-01T22:50:04.2133333+00:00

    Hi @Lloyd Sheen ,

    If I'm understanding your question correctly it is only loosely related to the title of your post. Your question is about handling assets from your APK rather than about the file system in general.

    Your scenario is that you have files which you would like to include in a folder in your app's assets so that you can copy these files into application data at runtime.

    It sounds like you have found the File System Helpers/Writing from a bundled file to the app data folder documentation which explains how to copy a single file from MauiAssets to the file system, but you are blocked by not knowing how to list the files in your Android app's assets.

    Here are three ways to do this:

    1. MauiAssets are constant once the package is built, so you can include a directory list when you build the package. This can be built automatically in a pre-build event)
    2. MAUI is .Net, so you can use .Net resources rather than Maui assets. Include the files as Embedded Resources rather than as MauiAssets and use GetManifestResourceNames to list them and GetManifestResourceStream to read them. Embedded resources are a .Net concept and not MAUI specific.
    3. MAUI apps can call native code, so you can list the Assets created in folders in the Resources/Raw/MyFiles folder with the Android AssetManager's ListAsync method.
        #if ANDROID
            Android.Content.Context ctx = Platform.AppContext;
            string[] assets = await ctx.Assets.ListAsync("MyFiles");
            foreach(string asset in assets)
            {
                System.Diagnostics.Debug.WriteLine($"Asset: {asset}");
            }
        #endif
    

    There is an enhancement request to dynamically enumerate MauiAssets at Provide the equivalent of GetManifestResourceNames for MauiAsset files #10816

    If this isn't what you are looking for or if you need more help then please edit your question to clarify what versions of Maui and Android you are using, the exact scenario you are trying to achieve, what code you've already tried, what results you got from that, and how those results differ from what you're trying to do.

    --Rob