Skip to content Skip to sidebar Skip to footer

Android.os.environment... Which Namespace?

I want to save some data to the storage (internal/external) of an Anroid device. I found this post where they got the external storage path with the following line: var sdCardPath

Solution 1:

Just typing the answer here so we can close this question...

The problem with what you are trying to do is that you need to understand that there is a separation of concerns/abstraction. The Core project is referenced by the Android & iOS projects, but the Android & iOS projects are not referenced by the Core project. Schematically,

Core-->Android & Core-->iOS but Core<-x-Android, Core<-x-iOS

This is by design, and even though you can make it do what you are trying, you shouldn't.

We know that the iOS & Android SDKs are very different. So in order to get the path/file name, you have to perform different functions underneath. And the code that you shared is the correct way of retrieving the path/file name natively ONLY for Android.

In order to make it work for both iOS & Android, Xamarin Forms provides us with an abstraction layer that takes care of everything underneath for us, and allows us to use this:

string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); 
var fileName=Path.Combine(filePath,".");

There's also many other ways of doing this as shown in the official Microsoft docs here.

Post a Comment for "Android.os.environment... Which Namespace?"