locked
The name 'Android' does not exist in the current context RRS feed

  • Question

  • User395718 posted

    Hey, im have an app to read a file. in MapPage.xaml.cs i use for android var filePath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; for Android this will work.

    when i want it to build for iOS i get the following error The name 'Android' does not exist in the current context using Xamarin.Forms.PlatformConfiguration will not work, because it will say .OS does not contain a definition in Android. for iOS i am using a different filePath.

    i need to use the ExternalStorageDirectory. Is it possible to make it working so iOS can somehow ignore the Android.OS part in the code?

    Wednesday, July 22, 2020 8:15 AM

Answers

  • User379860 posted

    You can use dependence service to get the FilePath from different platform.

    1.Create a interface in PCL. public interface IDeviceFilePathService { string GetFilePath(); }

    1. Get the path by following code. string str=DependencyService.Get<IDeviceFilePathService>().GetFilePath();

    2. Achieve IDeviceFilePathService interface in different platform.

    In Android. ``` [assembly: Dependency(typeof(DeviceFilePathService))] namespace MyEntry.Droid { public class DeviceFilePathService : IDeviceFilePathService { public string GetFilePath() {

          return  Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        }
    }
    

    } ```

    In IOS, you can retrun path what ever you want.

    [assembly: Dependency(typeof(DeviceFilePathService))] namespace MyEntry.iOS { class DeviceFilePathService : IDeviceFilePathService { public string GetFilePath() { string path=""; return path; } } }

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 22, 2020 10:28 AM

All replies

  • User379860 posted

    You can use dependence service to get the FilePath from different platform.

    1.Create a interface in PCL. public interface IDeviceFilePathService { string GetFilePath(); }

    1. Get the path by following code. string str=DependencyService.Get<IDeviceFilePathService>().GetFilePath();

    2. Achieve IDeviceFilePathService interface in different platform.

    In Android. ``` [assembly: Dependency(typeof(DeviceFilePathService))] namespace MyEntry.Droid { public class DeviceFilePathService : IDeviceFilePathService { public string GetFilePath() {

          return  Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        }
    }
    

    } ```

    In IOS, you can retrun path what ever you want.

    [assembly: Dependency(typeof(DeviceFilePathService))] namespace MyEntry.iOS { class DeviceFilePathService : IDeviceFilePathService { public string GetFilePath() { string path=""; return path; } } }

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 22, 2020 10:28 AM
  • User395718 posted

    @LeonLu Thanks its working :)

    Wednesday, July 22, 2020 12:03 PM