I'm in testing step of my MAUI application, and I have an issue when testing on a emulated Android 9 (API 28). I was testing and correcting my code on last Android version (Android 15, API 35), and then downloaded my APK to my phone which is under Android 9 (my old but still functioning S8+). And I had issue when Load/Save json file through my application. So I decided to create another emulated android device under Visual Studio, and create an Android 9 API 28 one.
I Then add some log/trace in my code to easier debugging and found the discrepancy causing the issue.
Here is a part of my saving code showing it :
FileSaverResult? savedFile;
using var memoryStream = new MemoryStream();
savedFile = await _fileSaver.SaveAsync(Path.GetDirectoryName(fullPath), Path.GetFileName(fullPath), memoryStream, cancellationToken);
memoryStream.Dispose();
if (savedFile == default || !savedFile.IsSuccessful ||savedFile.FilePath == default)
return;
DiagnosticsUtility.WriteLine($" --- Picked save file is : {savedFile.FilePath}");
DiagnosticsUtility.WriteLine($" --- File exist ? {File.Exists(savedFile.FilePath)}");
DiagnosticsUtility.WriteLine($" --- Directory exist ? {Directory.Exists(Path.GetDirectoryName(savedFile.FilePath))}");
and here are the log :
Android 15 (API 35)
--- Picked save file is : /storage/emulated/0/Documents/LotoRelease/Loto.json
--- File exist ? True
--- Directory exist ? True
Android 9 (API 28)
--- Picked save file is : /storage/home/LotoRelease/Loto.json
--- File exist ? False
--- Directory exist ? False
So returned path on Android 9 (API 28) for Documents folder is /storage/home which is not considered existing ?! Note that if select instead the Download folder, then I have the same, correct, result /storage/emulated/0/Download on both emulated Android version (9 and 15).
Since I want to offer the user of my application to save / load in any folder they want, and not only in Download folder, how can I get back the correct existing path on Android 9 ?
I did search on google for
"/storage/home" + Android
But found no result. What is this /storage/home path ?
I don't want to hard code a replacing of every path with /home to /emulated/0/Documents, I'd prefer a more generic solution.