How to access Android ExternalStorageDirectory on Cross apps

Hello,

Depending on device, i want to setup a "root path" for my app. I want it to be on external storage for Android devices (Please dont ask me why :smile: ).

So here is my code :

switch(Device.RuntimePlatform)
            {
                case Device.iOS:
                    curDeviceType = TypeDevice.DeviceIos;
                    ressource_prefix = "Portwin.iOS.";
                    root_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    break;

                case Device.Android:
                    curDeviceType = TypeDevice.DeviceAndroid;
                    ressource_prefix = "Portwin.Droid.";
                    root_path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
                    break;
            }

Problem : it doesnt compile on iOS, because "Android" does not exist in actual context.
How can I do that ?

Thanks.

Best Answer

  • deczalothdeczaloth DE ✭✭✭
    edited September 2018 Accepted Answer

    @Migok ,

    The name "Android" does not exist in actual context

    I get it. The problem is that though at runtime the case "Device.Android" will never be entered when compiling on iOS the code is still being observed by mac compiler...

    You can avoid that problem using Dependency Service: basically you write code in each project (ios and Android) and each project should implement an interface. When you use DependencyService.Get the right code will be called.

    Please refer to the Xamarin Docu for further details!

    Note: If this solves your problem accept the answer, otherwise reject it so that the question is still visible to other users ;)>


    EDIT: Your if/else block is just fine, and should run without problems, but is not elegant and you will find yourself soon with such kind of code in many places. In most cases DependencyService is THE WAY.

Answers

  • deczalothdeczaloth DEMember ✭✭✭

    Hi @Migok,

    I do not understand your problem:

    your code looks ok. What is the error/exception you are getting?

  • MigokMigok Member ✭✭

    Hello,

    Problem is during iOS compilation. He doesnt reconize "Android.OS" :

    The name "Android" does not exist in actual context

    I tried something else, with SAP conditionning, which looks to work :

    #if __IOS__
                curDeviceType = TypeDevice.DeviceIos;
                ressource_prefix = "Portwin.iOS.";
                root_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    #elif __ANDROID__
                curDeviceType = TypeDevice.DeviceAndroid;
                ressource_prefix = "Portwin.Droid.";
                root_path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    #endif           
    

    Should I be able to do that with PCL conditionning ?

  • deczalothdeczaloth DEMember ✭✭✭
    edited September 2018 Accepted Answer

    @Migok ,

    The name "Android" does not exist in actual context

    I get it. The problem is that though at runtime the case "Device.Android" will never be entered when compiling on iOS the code is still being observed by mac compiler...

    You can avoid that problem using Dependency Service: basically you write code in each project (ios and Android) and each project should implement an interface. When you use DependencyService.Get the right code will be called.

    Please refer to the Xamarin Docu for further details!

    Note: If this solves your problem accept the answer, otherwise reject it so that the question is still visible to other users ;)>


    EDIT: Your if/else block is just fine, and should run without problems, but is not elegant and you will find yourself soon with such kind of code in many places. In most cases DependencyService is THE WAY.

Sign In or Register to comment.