Search code examples
androidxamarinfileprovider

Xamarin Android FileProvider Java.Lang.IllegalArgumentException: Failed to find configured root that contains


I use Xamarin to create Android Apps. I want to open my app's file in other app (let say .docx file in microsoft word app, and etc). I use FileProvider, but always get this error:

Exception:Java.Lang.IllegalArgumentException: Failed to find configured root that contains...

This is my FileProvider file:

[Android.App.MetaData("android.support.FILE_PROVIDER_PATHS", Resource = "@xml/file_provider_path")]
[Android.Content.ContentProvider(new System.String[] { "${applicationId}.fileProvider" }, Exported = false, GrantUriPermissions = true, Name = "Android.Support.V4.Content.FileProvider")]
public  class AppFileProvider : Android.Support.V4.Content.FileProvider
{
    public AppFileProvider()
    {

    }

}

I also have .xml file with name file_provider_path.xml with content:

<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <files-path name="file_provider_path" path="/" />
</paths>

and to get file uri I use:

Java.IO.File _file = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "document1.docx");
AppFileProvider.GetUriForFile(this.ApplicationContext, this.Application.ApplicationContext.PackageName + ".fileProvider", _file)

Can you help me to find solution?


Solution

  • Based on Mike's Comment, I read this https://developer.android.com/reference/android/support/v4/content/FileProvider#SpecifyFiles . And I need to set "../" as my path in file_provider_path.xml, because I did not save the file on files folder.