I am trying to achieve the following in a Blazor Hybrid app for android, written in c# in VisualStudio 2022:
- make my app create and store a text file with some basis content (works)
- launch (using Launcher.Default.OpenAsync) or share (using Share.RequestAsync) the file, allowing the user to select an app to modify the text file
- allow the user to save the file in the same location as where my app put it
At step 2: When prompted, I choose QuickEdit. Launcher works as expected: my text file opens. Share fails, however: QuickEdit opens an empty file.
It fails at step 3: I get writing permission denied messages, with the suggestion to export the FileProvider (which is not possible: build fails if I try) or getUriPermission (which I tried to set)
Does anyone know how to do this in a Blazor Hybrid app?
Update
Trying to implement @blackapps comment, I changed my code to this:
var fn = "nieuw.txt";
var docsDirectory = Android.App.Application.Context.GetExternalFilesDir(null);
var file = Path.Combine($"{docsDirectory.AbsoluteFile.Path}", fn);
var myUri = Android.Net.Uri.Parse(file);
fileExists = File.Exists(file).ToString();
Android.Content.Intent viewDoc = new Android.Content.Intent(Android.Content.Intent.ActionView);
viewDoc.SetDataAndType(myUri,"text/plain");
viewDoc.SetFlags(Android.Content.ActivityFlags.NewTask);
viewDoc.AddFlags(Android.Content.ActivityFlags.GrantReadUriPermission);
viewDoc.AddFlags(Android.Content.ActivityFlags.GrantWriteUriPermission);
Android.App.Application.Context.StartActivity(viewDoc);
message = myUri.ToString();
I am prompted to choose an app (which is as intended) but the app opens an empty text file.
This, however, does work, but I can't save (in the same location) as explained before.
string popoverTitle = "Read text file";
var fn = "nieuw.txt";
var docsDirectory = Android.App.Application.Context.GetExternalFilesDir(null);
var file = Path.Combine($"{docsDirectory.AbsoluteFile.Path}", fn);
await Launcher.Default.OpenAsync(new OpenFileRequest(popoverTitle, new ReadOnlyFile(file)));
Any suggestions?