我正在开发一个Android应用程序,我想从启动MS办公应用程序来查看和编辑办公文件。例如,打开存储在设备本地的文档,以便在MS-Word移动应用程序中进行编辑。
以前我们打开这样的文件时,会在意图中传递一个文件URI,例如"com.microsoft.office.word“包名。该文件将在Word for Android中打开,用户可以对其进行编辑并保存。没问题。
现在,我们必须进行更改,以便使用具有读写权限的FileProvider class of Android。有了这个实现,其他应用程序可以编辑文件,但用于Android的Microsoft Office应用程序是以只读模式打开的,没有更改它的选项。
这似乎也是发生在其他人身上的常见问题,就像在其他堆栈溢出问题中看到的那样:
和
Xamarin.Forms Android FileProvider: GrantWriteUriPermission not always working
我也找到了这个link with information about how to invoke the office apps in msdn,但它似乎相当不完整,我无法让它与意图和本地文件一起工作(我只是不知道如何发送ms-word:ofe|u|文件以便它识别它,它总是抱怨找不到该文件)。
有没有人知道用FileProvider从微软android office的Android应用程序中以编辑模式打开本地文件的方法?
我没有发布任何代码,因为它是没有问题的。任何其他应用程序都可以正常运行,但Microsoft Office应用程序除外。
发布于 2017-11-07 07:09:18
我编写了一个通用的open例程,然后将单个文件类型分开,如下所示。完成以下操作后,我还没有遇到任何问题。希望这能有所帮助。(注意-我只添加了call这个词-但在这里使用了SO文章中的类型(What is a correct mime type for docx, pptx etc?))
public static Java.IO.File CopyDocuments(Java.IO.File source, string realName)
{
//string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); <-- old method
string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
if (!System.IO.Directory.Exists(Path.Combine(path, "appname")))
System.IO.Directory.CreateDirectory(Path.Combine(path, "appname"));
string dbPath = Path.Combine(path,"appname", realName);
Java.IO.File destination = new Java.IO.File(dbPath);
try
{
//if (destination.Exists())
// destination.Delete();
if (!destination.Exists())
{
using (FileStream fs = new FileStream(source.AbsolutePath, FileMode.Open, FileAccess.Read))
{
using (var br = new BinaryReader(fs))
{
using (var bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int length = 0;
while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, length);
}
}
}
}
}
return destination;
}
catch (Exception ex)
{
Toast.MakeText(Xamarin.Forms.Forms.Context, "Error Copying: " + ex.Message, ToastLength.Long).Show();
}
return null;
}
public void LaunchApp(string fileLocation, string realName)
{
var file = new Java.IO.File(fileLocation);
if (!file.Exists())
return;
file = CopyDocuments(file, realName);
Intent intent = null;
var extension = System.IO.Path.GetExtension(fileLocation).ToLower();
switch (extension)
{
case ContentService.DocxExtension:
intent = ReturnWord(file, true);
break;
case ContentService.DocExtension:
intent = ReturnWord(file, false);
break;
case ContentService.TxtExtension:
case PlayerLync.Services.ContentService.RtfExtension:
intent = ReturnText(file);
break;
case ContentService.XlsExtension:
intent = ReturnExcel(file, false);
break;
case ContentService.XlsxExtension:
intent = ReturnExcel(file, true);
break;
case ContentService.PPExtension:
intent = ReturnPowerPoint(file, false);
break;
case ContentService.PPXExtension:
intent = ReturnPowerPoint(file, true);
break;
case ContentService.Mp3Extension:
//contentType = ContentType.Audio;
break;
default:
//contentType = ContentType.Unknown;
break;
}
try
{
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
catch (Exception ex)
{
... log error
}
}
private Intent ReturnWord(Java.IO.File file, bool isEx)
{
var intent = new Intent(Intent.ActionView);
Android.Net.Uri uri = FileProvider.GetUriForFile(Xamarin.Forms.Forms.Context, "your_package.fileprovider", file);// --> old method Android.Net.Uri.FromFile(file); //
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);
intent.PutExtra(Intent.ExtraStream, uri);
if (!isEx)
{
intent.SetDataAndType(uri, "application/vnd.msword");
}
else
{
intent.SetDataAndType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}
return intent;
}把这一切都放在Android项目中,并做一个自定义的渲染器来从Xamarin表单访问它
https://stackoverflow.com/questions/45505243
复制相似问题