0

Neither Word nor Docs will open the file. Word gives a "Try Saving the File on the Device" error, and looking at the Docs log there's a Java Permission error.

Files are written to the public Pictures folder, and I can open them from Word on the device if I manually navigate to them, but a user should be able to click on a .Doc logo and open the file from the App.
I've added permissions flags and still no luck.

Paths:

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

Inside Application Tags in Manifest

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

Main function that opens a new intent (either Word or 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.
                    }
                }
TheRobQ
  • 108
  • 8

1 Answers1

0

So, it turns out that having two providers will not work, and removing the extraneous provider is what fixed this issue. I'll post the working code here for anyone that has run into this issue.

Paths are the same since we are saving these to an external public folder.

In Android Manifest:

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

Function that opens the file in a new App

                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.
                }
TheRobQ
  • 108
  • 8