Forum Cross Platform with Xamarin
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Error Xamarin.Essentials: File System Helpers on android device

Hallo together,

I'm getting on Debugging on Android device an IO error, when trying to read from the assets Directory. IOS is working fine. Do I miss still some permissions?

    private async void ReadFile(string sFile)
    {
        sFile = "test.txt";
        try
        {
            using (var stream = await FileSystem.OpenAppPackageFileAsync(sFile))
            {
                using (var reader = new StreamReader(stream))
                {
                    var fileContents = await reader.ReadToEndAsync();
                }
            }
        }
        catch (Exception e)
        {
            var error = e;
        }
    }

I Setup the permission in Android :

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        LoadApplication(new App());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

Thanks, :smile:

Markus

Posts

  • anteroantero Member ✭✭

    I firgured it our myself, I missed a permission in Android Manifest:

Sign In or Register to comment.