in my Xamarin Forms application I create pdfs that can be opened with Android application.
With device with Android 7 all works fine, now I have tested it with Android 8 and the file can't be open.
I have installed the Xam.PluginMedia and leave all default settings:
in the MainActivity I have override the OnRequestPermissionsResult function with:
Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
the file_paths.xaml is:
<?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" />
</paths>
and in the AndroidManifest.xml I have added:
<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>
The code to open the preview is this:
public void OpenPreviewFile(string FilePath)
{
Java.IO.File JavaFile = new Java.IO.File(FilePath);
Android.Net.Uri Path = FileProvider.GetUriForFile(global::Android.App.Application.Context, global::Android.App.Application.Context.PackageName + ".fileprovider", JavaFile);
string Extension = global::Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Path.ToString());
string MimeType = global::Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(Extension);
Intent Intent = new Intent(Intent.ActionView);
Intent.SetDataAndType(Path, MimeType);
Intent.AddFlags(ActivityFlags.GrantReadUriPermission);
Intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
try
{
MainActivity.AppInstance.StartActivity(Intent.CreateChooser(Intent, TS.Translate("ANDROID_ScegliApp")));
}
catch (System.Exception)
{
Toast.MakeText(MainActivity.AppInstance, TS.Translate("ANDROID_ImpossibileAprireIlFIle"), ToastLength.Short).Show();
}
}
The files generated are under this path:
Android/data/MY_PACKAGE_ID/files/GeneratedReports
I have attached a video to show the actual behavior.
Here a sample
I found the problem.
Inside the file_paths.xml
, the "path" value of the nodes must be started with "/",
so with this node:
<files-path name="GeneratedReports" path="/GeneratedReports" />
my code works.
Also I needed to remove MyIntent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);