7

I'm trying to write and read a file on the Android file system. A task you would think would be straight forward, but I just can't seem to find the right enumerator for a folder/path that actually exists.

I have read several posts on the same subject but none of them give a straight answer to this question and those that seem to have fixed their issues, has done so with paths that doesn't seem to work for me. Some suggestions seem to be related to System.IO while others seem to be of the Java equivalent.

What I'm trying to do is something as simple as to create a file somewhere in the internal storage (NOT external SD card or something) that is accessible from the device itself (via File Browser app or similar).

If that goes well (which it currently doesn't) then I would also like to read it later on.

I'm not looking for code to write or read files - I'm looking for something that can give me the correct path to a folder where I can do this.

I have tried many different variants, some of which are:

Environment.GetFolderPath (Environment.SpecialFolder.Personal);

returns the path: /data/data/my_app_full_name/files/

global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath.ToString();

returns the path: /storage/emulated/0/

Can anyone give a clear answer as to which enumerator to use for this? - would be much appreciated.

PS. I'm launching the app on an actual device (Android 5.0.2) from Xamarin Studio via USB.

EDIT: The answer, for me at least, is found under the comments for replay by misho.

1
  • Did you check my answer? Commented Mar 9, 2016 at 12:50

1 Answer 1

3

I think this is what you need:

Edit

public static string Directorypath { get { return System.IO.Path.Combine((string)Android.OS.Environment.ExternalStorageDirectory, "FolderPath"); } } 

enter image description here

Sign up to request clarification or add additional context in comments.

10 Comments

Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.ExternalStorageDirectory) doesn't work because GetExternalStoragePublicDirectory() takes a string as argument. So I tried Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath)) instead which gives me: /storage/emulated/0/storage/emulated/0 so also doesn't work.
ups, sorry, copied wrong code. updated answer, check it now
Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "Download") returns: "/storage/emulated/0/Download" BUT, if I use the on-device file browser and go to the folder Download, the path shown on top is in fact "/storage/emulated/0/Download". Why it has this odd actual path I cannot explain - the word "emulated" confuses for sure, because it makes you think it has to do with the emulator in some way, although this is one the device... So for now I'm gonna try a bit using this path and see how things go - thanks misho - I'm gonna test some more.
I'll try to explain that: if i remember correctly emulated stands for external storage, not the device itself. 0 defines user. there was Multi-User feature added from JellyBean 4.2 so if you add another user there will be another folder /storage/emulated/1/ and so on. To make it clearer: /storage/ - actual storage. /emulated/ - storage type (or smth, definitely not android emulator). /0/ - user. definition of emulated may not be 100% true but I'm pretty sure it has nothing to do with genymotion or any emulator.
that's how above screen looks from android device monitor: i.imgur.com/Jo0lJLs.png my device is galaxy note 4. you can find device monitor in /android-sdk/tools/ folder
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.