I am attempting to write a file on an Android device. I would then like to be able to easily transfer that file to a Windows computer for viewing and analysis.
Needless to say, I am having a lot of issues doing this. I am using .Net MAUI, and my Android device is a Samsung Galaxy Tab A7 Lite running Android 13.
I am running the following code to write a test file. I also have some code inserted as a sanity check to immediately read the file that I wrote:
string target_file = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "test_file.txt");
using (FileStream output_stream = System.IO.File.OpenWrite(target_file))
{
using (StreamWriter stream_writer = new StreamWriter(output_stream))
{
stream_writer.Write("hello, world!");
}
}
using (FileStream input_stream = System.IO.File.OpenRead(target_file))
{
using (StreamReader stream_reader = new StreamReader(input_stream))
{
var input_text = stream_reader.ReadToEnd();
}
}
This code is working. It is writing a file, and I am am able to read the file. The problem is that I am not able to see this file anywhere outside of the application. I can't see it in the Android "My Files" app on the Samsung tablet, nor can I see it in the Windows File Explorer when I connect my Android tablet to the PC.
The file is supposedly at the following path: "/data/user/0/[com.companyname.appname]/files/test_file.txt"
According to the comments in the following Stack Overflow posts, the "solution" is to save to a "shared" folder, because apparently the private app data folder is not viewable with Windows File Explorer:
- Creating and Writing to a text file on Android .NET MAUI
- .NET MAUI writing file in android to folder that then can be accessed in windows file explorer
Unfortunately, the answer posted on this post does not work:
.NET MAUI writing file in android to folder that then can be accessed in windows file explorer
It once again just directs the saved file to be in the app's private data folder. Specifically, it tries to save at this path:
"/storage/emulated/0/Android/data/[com.companyname.appname]/files/Documents"
One suggestions that has been made is to use the .Net MAUI FileSaver (https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/essentials/file-saver?tabs=android). While this may seemingly allow saving to a shared folder (I haven't yet tested it to fully find out), it would require user interaction to actually select the save location of the file, which is unacceptable in this scenario. My app needs to be able to save log files to a very specific folder at any time (without user interaction), and then periodically the user needs to be able to transfer those files to a computer for viewing/analysis.
Any suggestions on how to solve this issue?