.NET MAUI Android: Word documents opened via MediaStore do not save changes and makes copies

George Phillips 20 Reputation points
2025-09-22T06:34:52.7066667+00:00

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?
Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 4,520 Reputation points Microsoft External Staff Moderator
    2025-09-22T09:42:29.1133333+00:00

    Hello George Phillips,

    Thank you for reaching out.

    Am I missing a permission (e.g., write access via MediaStore)?

    No, what was missing was the Intent action. You should use ACTION_EDIT instead of ACTION_VIEW.

    Do I need to use a FileProvider URI instead of a MediaStore URI for Word to persist changes?

    No, MediaStore is the correct modern approach for files you want to be user-visible and stored in shared collections like "Documents". FileProvider is for sharing files from your app's private internal/external storage, which isn't what you're doing here.

    Is this a known limitation with Word on Android and MediaStore?

    No, I don't think so. It should work fine with the correct Intent action.

    I hope this helps. If my answer resolves your issue, please consider interacting with the feedback system.

    Thank you.


  2. Starry Night 615 Reputation points
    2025-09-25T02:49:18.0166667+00:00

    Syncfusion DocIO is a .NET MAUI Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can open and save a Word document in .NET MAUI.

    The following is the Add code example to save the Word document in .NET MAUI:

    //Saves the Word document to the memory stream.
    using MemoryStream ms = new();
    document.Save(ms, FormatType.Docx);
    ms.Position = 0;
    //Save the memory stream as a file.
    SaveService saveService = new();
    saveService.SaveAndView("Sample.docx", "application/msword", ms);
    

    You can also check a sample here: Open-and-save-Word-document

    For more information, you can check document Open and Save Word document in .NET MAUI.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.