I can not find a way how to open a file on Xamarin.Forms in Android from a dependency service.
By opening the file I mean showing the user the native UI to choose the which way he wants to open the file.
XPlat.Storage.IStorageFile storageFile = await blob.GetContentFileAsync();
File javaFile = new File(storageFile.Path);
Plugin.CurrentActivity.ICurrentActivity activity = Plugin.CurrentActivity.CrossCurrentActivity.Current;
Context context = activity.AppContext;
Uri path = FileProvider.GetUriForFile(context, context.PackageName, javaFile);
var intent = new Intent(Intent.ActionView);
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.ClearTop);
string mimeType = MimeTypes.GetMimeType(System.IO.Path.GetExtension(blob.UniqueName));
intent.SetDataAndType(path, mimeType);
context.PackageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.Activities);
context.StartActivity(intent);
An exception is thrown when invoking the method StartActivity(intent)
Android.Content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.company.appname/root/data/data/com.company.appname/files/Blobs/file.pdf typ=application/pdf }
I also tried using different approach to get the Application context -
var context = MonoAndroid.App.Application.Context;
, but it seems like the context obtained through Plugin.CurrentActivity
and this approach is the same.
new edit
Commenting out the line intent.SetDataAndType(path, mimeType);
results in following popup.