Error `Could not find a part of the path '/file:/android_asset'` - Android app

P5musicP5music Member ✭✭

I am using Visual Studio 2019 with Xamarin.
My simple prototype wants to have the list of folders I put in assets/resources directory.
The following methods are used:

` private string mainPath()
{
string path = "";

            switch (Device.RuntimePlatform)
            {
                case Device.Android:
                    path = "file:///android_asset/";
                    return path;
                case Device.UWP:
                    path = "ms-appx-web:///";
                    return path;
            case Device.iOS:
                path = NSBundle.MainBundle.BundlePath;
                return path;
                default:
                path = "";
                    return "";
            }
        }`

` public ItemsPage()
{
InitializeComponent();

            BindingContext = viewModel = new ItemsViewModel();
        string[] files= { };
        int size=0;
        string path = mainPath();
             Console.WriteLine("app output - main path " + path);
        try
        {
            files = System.IO.Directory.GetFiles(mainPath());
            size = files.Length;
            Console.WriteLine("app output - num files " + size);
        } catch (Exception e)
        {
            Console.WriteLine("app output - error " + e.Message);
        }

        string[] elements = new string[size];
        for (var i=0;i<size;i++)
        {
            elements[i] = files[i];
        }
        ItemsListView.ItemsSource = elements;

    }`

I can test only on my Android device. I get this error:
Could not find a part of the path '/file:/android_asset'
I do not understand. The folder is app private so no permission should be needed.
I have to say, as an Android developer, I would not use that string literal directly, but I was said to do so in another thread.
Said that, now I want my app to work. What is the problem?

Best Answer

Answers

  • P5musicP5music Member ✭✭
    edited August 2

    Ok, that path is not accessible.
    So I used this method:

       private String[] folderEntryListinAssets(String folderName)
            {
    
    
                switch (Device.RuntimePlatform)
                {
                    case Device.Android:
                    Context context = Android.App.Application.Context;
    
                    String[] list=context.Assets.List(folderName);
                    Console.WriteLine("app output - list size " + list.Length);
                    for (int i = 0; i < list.Length; i++)
                    { Console.WriteLine("app output - entry "+list.GetValue(i).ToString()); }
    
    
                    return list;
                   /* 
                    case Device.UWP:
    
                        toDo
    
                    case Device.iOS:
    
                   toDo
                   */
                    default:
    
                    return null;
                 }
             }
    

    `

    If I call it wih "/" as parameter I get a list of entries like lib, assets, META-INF and so on.
    I think the Assets manager is not about the assets folder but a general app folder.
    Said that, if I call the above mentioned method with "/assets/" I get nothing.
    Why? Is anybody able to help me?

  • WendyZangWendyZang Member, Xamarin Team Xamurai

    Based on my test, I reproduce the exception.

    You can try to change the following code.
    Change:
    files = System.IO.Directory.GetFiles(mainPath());
    To:
    var files = Android.App.Application.Context.Assets.List("");
    This will list all the files in Asset include some hidden files.

    Or you could specify the folder in Asset.
    var files = Android.App.Application.Context.Assets.List("Folder1");
    This will list all the files in Folder named Folder1.

    The Assets.List() method directs to the Asset, there is no need to use “/asset/” again.

  • P5musicP5music Member ✭✭

    @WendyZang
    I see a file list that is not of the assets folder, I find the assets folder itself in that list, and the manifest, lib, etc.

Sign In or Register to comment.