Error processing file through intent filter .Net MAUI

Влад Тигинян 40 Reputation points
2023-06-30T08:24:10.5066667+00:00

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.

User's image

Developer technologies .NET .NET MAUI
Developer technologies .NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Влад Тигинян 40 Reputation points
    2023-07-06T11:15:03.8966667+00:00

    This is the Nuget package https://github.com/LuckyDucko/Mopups

    I solved the problem with intent filters by adding a static property to the file App.xaml.cs and calling it at the moment the page is loaded.

    public partial class App : Application
    {
    ...
    	#if ANDROID
    	    public static Android.Net.Uri Uri { get; set; }
    	#endif
    ...
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.