Xamarin Android file system paths
I think this is what you need:
Edit
public static string Directorypath
{
get
{
return System.IO.Path.Combine((string)Android.OS.Environment.ExternalStorageDirectory, "FolderPath");
}
}
Aidal
Updated on June 20, 2022Comments
-
Aidal almost 2 years
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.
-
Aidal about 8 yearsAndroid.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.
-
arsena about 8 yearsups, sorry, copied wrong code. updated answer, check it now
-
Aidal about 8 yearsPath.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.
-
arsena about 8 yearsI'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 ofemulated
may not be 100% true but I'm pretty sure it has nothing to do withgenymotion
or any emulator. -
arsena about 8 yearsthat's how above screen looks from
android device monitor
: i.imgur.com/Jo0lJLs.png my device isgalaxy note 4
. you can finddevice monitor
in/android-sdk/tools/
folder -
Aidal about 8 yearsIf by external you mean SC-card then it makes no sense, because this phone has no SD-card inserted. I really would like to target the device internal memory. Or did you mean something else by external?
-
arsena about 8 yearsI meant storage type as i said below
-
arsena about 8 yearsOk, here it is:
Emulated storage is provided by exposing a portion of internal storage through an emulation layer and has been available since Android 3.0.
The "/storage/emulated/" folder does not really exist. It's what might be called a "symbolic link", or, in simpler terms, a reference to where the real data is stored.
-
arsena about 8 yearsmore:
If your Android only has internal memory and not a physical internal storage separate from it, it creates a logical partition out of it and makes it work like internal storage.
References: source.android.com/devices/storage superuser.com/questions/949659/… quora.com/… -
Aidal about 8 yearsAha! so the path /storage/emulated/0/ is actually quite ok. Thanks for explaining this misho!