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:
intent.SetAction(Intent.ActionEdit);
does not make a differenceintent.SetDataAndType(uri, "application/msword");
does not make a differenceActionView
& SetDataAndType
as it is used in the file explorer does not work eitherlike 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):
File-Explorer (Word file can be edited):
intent.SetDataAndType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
does not make a differenceIt 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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With