Word和Docs都不会打开文件。 Word给出“尝试在设备上保存文件”错误,并且在文档日志中看到Java权限错误。
文件被写入公共图片文件夹,如果手动导航到文件,则可以从设备上的Word中打开它们,但是用户应该能够单击.Doc徽标并从中打开文件该应用程序。我添加了权限标志,但仍然没有运气。
路径:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
<external-path name="external_files" path="." />
</paths>
清单中的内部应用程序标签
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.company.app.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
</provider>
打开新意图的主要功能(Word或Docs)
var filename = "AFileName"
var localPhotoPath = System.IO.Path.Combine(global::Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath, "workOrder");
localPhotoPath = System.IO.Path.Combine(localPhotoPath, "filename");
if (filename.Contains(".docx"))
{
Intent viewIntent = new Intent(Intent.ActionEdit);
//viewIntent.SetAction();
Java.IO.File exportFile = new Java.IO.File(localPhotoPath);
exportFile.SetReadable(true);
var photoURI = FileProvider.GetUriForFile(Application.Context, "com.company.app.fileprovider", exportFile);
viewIntent.SetDataAndType(photoURI, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
viewIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
viewIntent.AddFlags(ActivityFlags.GrantWriteUriPermission);
viewIntent.AddFlags(ActivityFlags.NewTask);
var resolved = PackageManager.QueryIntentActivities(viewIntent, 0);
if (resolved != null && resolved.Count > 0)
{
foreach (var t in resolved)
{
Application.Context.GrantUriPermission(t.ActivityInfo.PackageName, photoURI, ActivityFlags.GrantReadUriPermission);
StartActivity(viewIntent);
}
StartActivity(viewIntent);
}
else
{
// notify the user they can't open it.
}
}
因此,事实证明,拥有两个提供程序将not起作用,而删除无关的提供程序则可以解决此问题。我将在这里发布适用于遇到此问题的任何人的工作代码。
路径是相同的,因为我们将它们保存到外部公用文件夹。
在Android清单中:
<application android:allowBackup="true" android:icon="@mipmap/VamsLauncher" android:label="@string/app_name" android:roundIcon="@mipmap/VamsLauncher" android:supportsRtl="true" android:theme="@style/AppTheme">
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
在新应用中打开文件的功能
Intent viewIntent = new Intent(Intent.ActionView);
//viewIntent.SetAction();
Java.IO.File exportFile = new Java.IO.File(localPhotoPath);
exportFile.SetReadable(true);
var photoURI = FileProvider.GetUriForFile(Application.Context, Application.Context.ApplicationContext.PackageName + ".fileprovider", exportFile);
viewIntent.SetDataAndType(photoURI, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
viewIntent.AddFlags(ActivityFlags.NewTask);
viewIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
viewIntent.AddFlags(ActivityFlags.GrantWriteUriPermission);
var resolved = PackageManager.QueryIntentActivities(viewIntent, 0);
if (resolved != null && resolved.Count > 0)
{
foreach (var t in resolved)
{
Application.Context.GrantUriPermission(t.ActivityInfo.PackageName, photoURI, ActivityFlags.GrantReadUriPermission);
}
StartActivity(viewIntent);
}
else
{
// notify the user they can't open it.
}