11895

Android.OS.Environment… which namespace?

<h3>Question</h3>

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 = Android.OS.Environment.ExternalStorageDirectory.Path;

Seems fair. But my Visual Studio says the following:

<blockquote>

The name 'Android' does not exist in the current context

</blockquote>

So I've been wondering in which namespace does this Android reside? I tried quite a lot (e.g. AutoImport, but that seems to give me the wrong parent; or pressing F1 for documentation like suggested here) but didnt figure out how to get this to work. Which NuGet- Package do I have to get to obtain this function. Or am I doing something completely wrong?

This is the namespace that AutoImport gives me:

Xamarin.Forms.PlatformConfiguration.Android.OS.Environment.ExternalStorageDirectory.Path;

but then it tells me that Android has no OS member. And upon checking, I found that Xamarin.Forms.PlatformConfiguration.Android has no members at all, so theres nothing beneath it. Is my Xamarin.Forms depricated? According to my NuGet- Packagemanager, everything is up to date.

These are the NuGet- Packages my project uses:

<ol><li>Xamarin.Essentials</li> <li>Xamarin.Forms</li> <li>OxyPlot.Core</li> <li>OxyPlot.Xamarin.Forms</li> </ol>
<h3>Answer1:</h3>

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.

来源:https://stackoverflow.com/questions/61018488/android-os-environment-which-namespace

Recommend

  • post video to youtube
  • Zend Framework in a subfolder
  • count unique rows in sql-alchemy
  • NSOperationQueue and UITableView release is crashing my app
  • codeblocks and C undefined reference to getline
  • Why net().get on success return empty string
  • Gradle task for local tomcat deployment
  • Remove an overlay in mapKit Swift
  • Java XML DTD validation
  • Getting memory usage Live/Dirty Bytes in iOS app programmatically (not Resident/Real Bytes)
  • Split an Array into 3 arrays [duplicate]
  • Was default_marker removed from mapbox-gl.js
  • Storyboard iOS MBProgressHUD
  • How to add CKEditor RTE to typo3 Backend Module with the API?
  • How to get a Builder object from rows related to pivot - Laravel
  • How do I detect if an email client is configured on an Android device?
  • JSF validateLength question
  • Redmine can't generate secret token
  • Launch Dash from Jupyter Notebook
  • Caffe : train network accuracy = 1 constant ! Accuracy issue
  • Sprite animation wobbly / jumping in IE11
  • Ways of filling 10 places with number from [1..10] such that digit at ith place has value atmost 1 m
  • Generate a runnable jar and include libraries in it with Maven
  • create circular Auto Horizontal Scroll View?
  • jQuery colorbox breaks postbacks in ASP.NET Web Forms
  • Yii Bootstrap not loading JS files
  • Showing image on a acro text field position
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Julia 1.0 UndefVarError - Scope of Variable
  • Creating 2d platforms using JavaScript
  • How to merge objects within array based on attribute
  • How to write seo friendly url's using htaccess?
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Error handeling in antlr 3.0
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • Print Longitude and Latitude in textbox Android
  • ssh remote server login script
  • customize soft keyboard key preview
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • How to check if object is null in Java?