Word 文档在使用 Intent 打开时始终为只读

编程入门 行业动态 更新时间:2024-10-18 18:18:15
本文介绍了Word 文档在使用 Intent 打开时始终为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试使用 MS-Word 在我的 Xamarin.Android 应用程序中打开和编辑位于外部存储中的 word 文档这段代码:

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);

问题是,该文件总是只读.当我通过任何文件浏览器(例如 File Manager Pro)打开文档时,它非只读,我可以编辑该文件.

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.

我是否缺少需要在我的应用或 FileProvider 中设置的任何权限?

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

我在 Android Word 应用程序中使用我的 Office 帐户登录使用WPS Office 效果很好,所以一定是 MS-Word 的特殊问题使用 intent.SetAction(Intent.ActionEdit); 没有区别使用 intent.SetDataAndType(uri, "application/msword"); 没有任何区别使用 Google 文档 的类似行为:使用 FileExplorer 我能够打开和编辑文件.如果我通过我的应用程序使用 Google 文档打开文件,我可以编辑该文件,但如果我点击保存,应用程序会要求我将文件更新为新的 DOCX 格式".然后我可以将文件保存到一个新目录.ActionView & 的组合SetDataAndType 因为它在文件资源管理器中使用也不起作用 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

喜欢这个

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

<小时>

意图比较:

我使用了意图拦截应用来比较来自我的应用的意图与来自文件浏览器的意图:

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

我的应用程序(Word 文件是只读的):

File-Explorer(可以编辑 Word 文件):

使用文件浏览器中的 MIME 类型 intent.SetDataAndType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); 没有区别

推荐答案

如果你使用 ActionEdit 而不是 ActionView 似乎可以工作,而不是

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

intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);

有了这个,我可以打开一个可编辑的 .doc 文件,但是,我无法将文件保存到原始位置(在我的应用程序的沙箱中).Word 将始终建议保存副本.经过几天的挠头,我怀疑这是微软的设计,我们的代码没有任何问题,尽管我不能确定......

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...

这篇关于Word 文档在使用 Intent 打开时始终为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 18:09:36,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   Word   Intent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!