Android Pie - System.Reflection.TargetInvocationException

ArselanArselan Member ✭✭
edited August 8 in Xamarin.Forms

Hello everyone,
I got an exception when trying to compile my Xamarin Forms app on Nexus 5X emulator (android Pie),
So the exception appears on this line below

public partial class App : Application { public static ILogger logger = DependencyService.Get<ILogManager>().GetLog();

I'm using DependencyService to get the Logging Functionality from IOS/ANDROID projects... I'm facing this problem only with android 9.0 (Pie) !!
this is the exception:

System.Reflection.TargetInvocationException Message=Exception has been thrown by the target of an invocation.

Any suggestions plz? thnx

Best Answer

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Firstly, Update your IDE and Xamarin forms nuget to the latest.

    Please add a breakpoint in your DependencyService‘s code, to narrow down which line make the exception.

  • ArselanArselan Member ✭✭
    edited August 11

    @LeonLu thanks for ur answer,
    I found where the exception is coming from...

    string pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "Folder Name"); Directory.CreateDirectory(pathToNewFolder);

    Exactly when trying to create the directory !
    In my device (android 5.1) it works fine... even on android 8 !

    I already have the permissions to read/write on external storage !

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    This is the exception message:
    "Access to the path '/storage/emulated/0/Folder Name' is denied."

    thanks :)

  • ArselanArselan Member ✭✭

    Hey @LeonLu thanks I can now create a directory for my log files...
    But one more thing plz... Why I should enable storage permission manually (from app settings) every time I install the app... is there another solution to automatically create the folder or at least ask permisssion from the user ?

  • AlbertKAlbertK MYMember ✭✭✭

    May be something like this where permission is requested via the Android system

    protected override void OnCreate(Bundle savedInstanceState)
    {
    
       TabLayoutResource = Resource.Layout.Tabbar;
       ToolbarResource = Resource.Layout.Toolbar;
    
       base.OnCreate(savedInstanceState);
    
    
       global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
       global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    
      if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.M )
          { CheckPermissions(); }
    
           LoadApplication(new App() );
    }
    
    
    private readonly string[] Permissions =
    {
      Manifest.Permission.WriteUserDictionary,
      Manifest.Permission.WriteExternalStorage,
      Manifest.Permission.ReadExternalStorage,
    
    };
    
    private void CheckPermissions()
    {
        bool minimumPermissionsGranted = true;
    
        foreach (string permission in Permissions)
        {
            if (CheckSelfPermission(permission) != Permission.Granted) minimumPermissionsGranted = false;
        }
    
        // If one of the minimum permissions aren't granted, we request them from the user
        if (!minimumPermissionsGranted) 
            RequestPermissions(Permissions, 0);
    }
    
Sign In or Register to comment.