Reputation: 31
I am trying to generate a pdf file in my Xamarin form app. I am using the Syncfusion library for this.
But I got on FileProvider.GetUriForFile()
method the error :
"Failed to find configured root that contains /data/data/com.companyname.myappname/files/Syncfusion/Output.pdf"
Here my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.myappname">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
<application android:label="myappname.Android" android:theme="@style/MainTheme">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
My provider_paths :
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="external_files"
path="." />
</paths>
and my method call :
public async Task SaveAndView(string fileName, String contentType, MemoryStream stream)
{
string exception = string.Empty;
string root = null;
if (ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.WriteExternalStorage) != Permission.Granted)
{
ActivityCompat.RequestPermissions((Android.App.Activity)Forms.Context, new String[] { Manifest.Permission.WriteExternalStorage }, 1);
}
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//root = Android.OS.Environment.ExternalStorageDirectory.ToString();
}
else
{
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion");
myDir.Mkdir();
Java.IO.File file = new Java.IO.File(myDir, fileName);
if (file.Exists())
{
file.Delete();
}
try
{
FileOutputStream outs = new FileOutputStream(file);
outs.Write(stream.ToArray());
outs.Flush();
outs.Close();
}
catch (Exception e)
{
exception = e.ToString();
}
if (file.Exists() && contentType != "application/html")
{
try
{
string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionView);
intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
Android.Net.Uri path = FileProvider.GetUriForFile(Forms.Context, Android.App.Application.Context.PackageName + ".provider", file);
//Android.Net.Uri path = FileProvider.GetUriForFile(Forms.Context, BuildConfig.ApplicationId + ".provider", file);
intent.SetDataAndType(path, mimeType);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
}
I checked all the stackoverflow topic about this but I am still getting error :/
Also I am quit new to Xamarin.Android (only used the crosscode before)
Thanks !
Upvotes: 0
Views: 592
Reputation: 106
On further analysis from the given code, we found that you have modified the folder path (root) in SaveAndView method of Android.cs file in the reference of the below KB documentation.
https://www.syncfusion.com/kb/9174/how-to-create-a-pdf-file-in-xamarin
Due to this changes only the reported issue occurs. Could you please try the below code in provider_paths.xml under the Android project Resource folder since this will work for the provided SaveAndView method code.
<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root" path="."/>
</paths>
Upvotes: 2