Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xamarin - Access the Android Special Folder Path by using Environment

I want to save my logs to a folder which I can access with windows explorer. For example I want to create my log in the following path

This PCGalaxy A5 (2017)PhoneAndroiddataMyAppfiles

So I tried to use Environment variables... I get such as

/data/user/...

But here i cannot see the file what I created (using code I can access the path but I want to see in the explorer).

how I can create a path like above with code?

When I tried this code

var finalPath2 = Android.OS.Environment.GetExternalStoragePublicDirectory
(Android.OS.Environment.DataDirectory.AbsolutePath);

I get the path "/storage/emulated/0/data"

and

If i use the code

   var logDirectory =Path.Combine(System.Environment.GetFolderPath
  (System.Environment.SpecialFolder.ApplicationData),"logs");

I get the following path like:

/data/user/0/MyApp/files/.config/logs

and

var logDirectory =Path.Combine(System.Environment.GetFolderPath
  (System.Environment.SpecialFolder.MyDocuments),"logs");

"/data/user/0/IM.OneApp.Presentation.Android/files/logs"

but unfortunately I cannot access this folder by explorer....

This PCGalaxy A5 (2017)PhoneAndroiddataMyAppfiles

So how to find out this path in c# by using environments?

Update:

when I give the following path hardcoded, it creates the file where I want..

logDirectory = "/storage/emulated/0/Android/data/MyApp/files/logs";

is there any environment to create this path? I can combine 2 environments and do some string processing in order to create this path. But maybe there is an easier way?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are looking for the root of GetExternalFilesDir, just pass a null:

Example:

var externalAppPathNoSec = GetExternalFilesDir(string.Empty).Path;

Note: This is a Context-based instance method, you can access it via the Android application context, an Activity, etc... (see the link below to the Android Context docs)

  • Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using Environment.getExternalStorageState(File).

  • There is no security enforced with these files. For example, any application holding Manifest.permission.WRITE_EXTERNAL_STORAGE can write to these files.

re: https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...