Idea is to save files inside Phone/Android/data/<app_package>/… directory and check them via File manager, like other application data is stored in phone e.g. Phone/Android/data/com.whatsapp and so on.
however I cannot target that location.
I’ve got included permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
also set preffered Install location = preferExternal
in manifest.
protected static readonly string _root = Path.Combine(
#if ANDROID
Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath,
#endif
"Library"
);
...
Directory.CreateDirectory(_root);
File.WriteAllBytes(
Path.Combine(_root, "test.txt"),
Encoding.UTF8.GetBytes("red panda")
);
here i expect Phone/Android/data/com.myApp/files/Library/test.txt to be created. However when i open that using File manager files dir is empty.
Tried FileSystem.Current.AppDataDirectory
but still
4
Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath
You’re storing your file on External app-specific directory. Files here are private, accessible by the app but are not directly visible in file managers due to Scoped Storage Restrictions. Storing files to this path doesn’t even require Storage permissions.
If you want your files to be public and accessible for phone File Manager. You need to store you files in External storage public directory. Ex: Downloads
string downloadsPath = global::Android.OS.Environment.GetExternalStoragePublicDirectory(global::Android.OS.Environment.DirectoryDownloads)?.AbsolutePath;
string filePath = Path.Combine(downloadsPath, "example.txt");
File.WriteAllText(filePath, "Hello, Downloads!");
This will write the file to your device downloads folder.
OR
if want see your files in your app specific directories through phone file manager you can store you files in ObbDirectory or MediaDirectory of external storage.
string obbDirPath = Android.App.Application.Context.ObbDir.AbsolutePath;
string mediaDirPath = Android.App.Application.Context.GetExternalMediaDirs()?.First()?.AbsolutePath;
string filePath = Path.Combine(mediaDirPath, "example.txt");
File.WriteAllText(filePath, "Hello, Media!");
filePath = Path.Combine(obbDirPath, "example.txt");
File.WriteAllText(filePath, "Hello, Obb!");
4