Hello. I'm trying to implement functionality for opening and displaying documents through the intent filter and from the application itself. Opening and displaying the file from within the application itself is no problem, but there is an issue with the intent filter.
So, I will describe my actions.
Added an intent filter to the MainActivity
[IntentFilter(new[] { Intent.ActionView, Intent.ActionMain },
Categories = new[]
{ Intent.ActionView,
Intent.ActionSend,
Intent.CategoryDefault,
Intent.CategoryBrowsable },
DataMimeType = "application/pdf")]
In the platform dependent part of the application, I overridden the method OnCreate()
Intent intent = this.Intent;
if (intent.Action == Intent.ActionView || (intent.Action == Intent.ActionMain && intent.DataString != null))
{
Android.Net.Uri uri = intent.Data;
WeakReferenceMessenger.Default.Send(new LoggedInUserChangedMessage(uri));
}
Next, a Android.Net.Url to the file is passed to the common part of the application
protected override void OnAppearing()
{
base.OnAppearing();
#if ANDROID
if (WeakReferenceMessenger.Default.IsRegistered<LoggedInUserChangedMessage>(this))
return;
WeakReferenceMessenger.Default.Register<LoggedInUserChangedMessage>(this, async (r, m) =>
{
var filePath = Editor.Instance.OpenNamedFile(m.Value);
if (BindingContext is MainPageViewModel viewModel)
{
viewModel.LoadDocumentAsync(filePath);
}
});
#endif
}
In method LoadDocumentAsync(filePath) processes the file asynchronously.
public async void LoadDocumentAsync(string filePath)
{
var documentProperties = DocumentRepository.Instance.LoadProperties(filePath);
if (documentProperties.Type == TypeOfFile.Undefined)
{
await Shell.Current.DisplayAlert(title: "", message: "", "ОК");
return;
}
if (!string.IsNullOrEmpty(filePath))
{
ClearCollections();
}
DocumentTitle = documentProperties.Name;
DocumentType = documentProperties.Type;
FilePath = documentProperties.Source;
IsVisibleMainInfo = true;
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(async () =>
{
if (!CheckInternet.IsConnectedMAUI())
{
await MopupService.Instance.PushAsync(new BaseMopupPage(
#if DEBUG
title: "",
#else
title: "",
#endif
description: "",
image: "error.png",
isclose: true));
await Task.Delay(3000);
await MopupService.Instance.PopAsync();
}
if (!CheckInternet.IsConnectedMAUI() && DocumentType == TypeOfFile.EDoc)
{
LoadSignatures(filePath);
TreatmentSignatures(arraySignatures);
}
if (CheckInternet.IsConnectedMAUI())
{
LoadSignatures(filePath);
TreatmentSignatures(arraySignatures);
}
if (DocumentType == TypeOfFile.EDoc)
AttachmentFiles = TreatmentAttachments(eDocument);
IsComponentVisible = true;
});
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
}
If the application was closed when the file was opened, nothing happens. The application starts but the file data is not displayed.
If the application was running and I want to open the file through the intent filter, then a fatal error occurs when executing the asynchronous method.
