Hello,
I’m building a .NET MAUI app (Android) that integrates with Word. The app downloads a .docx file, saves it to the device using MediaStore (so users can do in-place edits with Microsoft Word).
The issue:
- The Word document opens fine.
- Users can edit in Microsoft Word.
- But when they press Save in Word, the changes are not persisted — reopening the file in my MAUI app shows the old version.
- Each time the user re-opens the file a number is appended to the file name "(1)" meaning a copy was created and the original file was not edited.
private async Task<Uri?> CreateTempRowForWord(string sourcePath, Enumerables.FileLocation fileType)
{
var cr = Platform.CurrentActivity.ContentResolver;
var collection = MediaStore.Files.GetContentUri(MediaStore.VolumeExternalPrimary);
string baseName = Path.GetFileNameWithoutExtension(sourcePath);
string displayName = baseName + ".docx";
string relativePath = fileType switch
{
Enumerables.FileLocation.Document => $"Documents/{Platform.CurrentActivity.PackageName}/Documents",
_ => $"Documents/{Platform.CurrentActivity.PackageName}/Notes"
};
var values = new ContentValues();
values.Put(MediaStore.IMediaColumns.DisplayName, displayName);
values.Put(MediaStore.IMediaColumns.MimeType, DOCX_MIME);
values.Put(MediaStore.IMediaColumns.RelativePath, relativePath);
values.Put(MediaStore.IMediaColumns.IsPending, 1);
Uri? uri = cr.Insert(collection, values);
if (uri == null) return null;
try
{
// Copy canonical into temp row
using var input = System.IO.File.OpenRead(sourcePath);
using var output = cr.OpenOutputStream(uri, "w");
if (output == null) return null;
input.CopyTo(output);
}
finally
{
var finish = new ContentValues();
finish.Put(MediaStore.IMediaColumns.IsPending, 0);
cr.Update(uri, finish, null, null);
}
return uri;
}
private async Task OpenWordDocuments(string canonicalPath, Enumerables.FileLocation fileType)
{
var cr = Platform.CurrentActivity.ContentResolver;
var currentActivity = Platform.CurrentActivity;
Uri? tempUri = await CreateTempRowForWord(canonicalPath, fileType);
if (tempUri == null) return;
var intent = new Intent(Intent.ActionEdit)
.SetDataAndType(tempUri, DOCX_MIME);
intent.AddFlags(ActivityFlags.GrantReadUriPermission |
ActivityFlags.GrantWriteUriPermission |
ActivityFlags.NewTask);
intent.ClipData = ClipData.NewUri(cr, "doc", tempUri);
MainActivity._wordClosedTcs = new TaskCompletionSource<bool>();
MainActivity.WaitingForWord = true;
currentActivity.StartActivity(intent);
await MainActivity._wordClosedTcs.Task;
using (var input = cr.OpenInputStream(tempUri))
using (var output = System.IO.File.Open(canonicalPath, FileMode.Create, FileAccess.Write))
{
if (input != null) input.CopyTo(output);
}
cr.Delete(tempUri, null, null);
}
What I expected
After editing in Word and pressing Save, the changes should persist in the same file, so the MAUI app can reload the updated content.
What actually happens
The Word app allows editing, but on Save, the changes don’t persist because duplicates are created.
Re-opening the file from my app still shows the old content.
Question
- Am I using MediaStore correctly?
- Is it possible to reuse URI's stored in MediaStore to bypass the duplicate issue?