10

I am trying to open and edit a word document located in my external storage in my Xamarin.Android App with MS-Word with this code:

File wordFile = new File(wordFilePath);
wordFile.SetWritable(true);

FileInfo fileInfo = new FileInfo(wordFilePath);
fileInfo.IsReadOnly = false;

Android.Net.Uri uri = FileProvider.GetUriForFile(Context, Context.PackageName + ".provider", wordFile);

Intent intent = new Intent();
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);
intent.SetAction(Intent.ActionView);

intent.SetData(uri);

Context.StartActivity(intent);

The problem is, that the file is always Read-Only. When I open the document via any file explorer (e.g. File Manager Pro) , it is not Read-Only and I can edit the file.

Am I missing any permission that i need to set in my app or FileProvider?

EDIT:

  • I am logged in with my Office Account in the Android Word App
  • Using WPS Office works just fine, so it has to be a special problem with MS-Word
  • using intent.SetAction(Intent.ActionEdit); does not make a difference
  • using intent.SetDataAndType(uri, "application/msword"); does not make a difference
  • similar behavior using Google Docs: Using a FileExplorer I am able to open and edit the file. If I open the file with Google Docs through my app I can edit the file, but if I click Save, the app asks me to "Update the file to the new DOCX-Format". Then I can save the file to a new directory.
  • The combination of ActionView & SetDataAndType as it is used in the file explorer does not work either

like this

intent.SetAction(Intent.ActionView);
intent.SetDataAndType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

Intent Comparision:

I used an Intent Intercept App to compare the intent from my app with the intent from a file explorer:


My App (Word file is Read-Only): enter image description here


File-Explorer (Word file can be edited): enter image description here

  • using MIME-type from the file explorer intent.SetDataAndType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); does not make a difference
Aiko West
  • 791
  • 1
  • 10
  • 30
  • try to use `Intent.ActionEdit` instead of `Intent.ActionView` – Leo Zhu May 27 '19 at 03:18
  • I already tried that, does not help – Aiko West May 27 '19 at 04:36
  • intent.SetDataAndType(uri, "application/msword"); set type to have a try – Leo Zhu May 27 '19 at 04:54
  • i tried that too – Aiko West May 27 '19 at 05:20
  • Just to be clear... you tried replacing `SetData()` with `SetDataAndType()` and set your MIME type to that really long value from the second screenshot? (I'm not a Xamarin user, but I assume that they have a `SetDataAndType()` to match `SetData()`, the way we do in Java/Kotlin). If you have tried this, and it still did not work, you might want to update your question with the revised code and screenshots. – CommonsWare May 27 '19 at 22:04
  • yes, I mentioned that in the last point below the screenshots – Aiko West May 28 '19 at 04:31
  • but i added the new point in the edit section to make it clear :) – Aiko West May 28 '19 at 04:36
  • it's launch flags `1000 0000` vs. `1300 0000`, whatever `3` might be. – Martin Zeitler Jun 01 '19 at 14:43
  • You can refer the following for the writing purpose - https://stackoverflow.com/a/10667865/10941112 – Saswata Jun 02 '19 at 02:52
  • @K.Dexter Try to launch only with **intent.SetFlags(ActivityFlags.NewTask);** Remove all **AddFlags** calls. Let us know if that helps. – mmmatey Jun 02 '19 at 14:56
  • Duplicated. https://stackoverflow.com/questions/40157389/when-using-android-file-provider-files-dont-have-correct-permissions-despite-f – SeyyedMojtaba Jun 03 '19 at 03:14
  • @mmmatey your suggestion leads to an error in word: **Can't open file | Try saving the file on the device and then opening it** – Aiko West Jun 03 '19 at 04:59
  • 1
    Just wrote a technical documentation what I still found out: https://github.com/cryptomator/cryptomator-android/issues/150#issuecomment-514401775 Microsoft has to fix this problem as soon as possible! – Jules Jul 23 '19 at 22:13

1 Answers1

1

It seems to work if you use ActionEdit instead of ActionView, and NOT

intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);

With this, I am able to open a .doc file that is editable, however, I am not able to save the file to the original location (in my app's sandbox). Word will always suggest saving a copy. After scratching my head for days, I suspect this is by design by Microsoft, not anything wrong in our code, though I can't be sure...

Ben
  • 41
  • 3