Louis
Louis

Reputation: 11

File Association in Xamarin Forms (Android not working)

The application needs to receive XML Files from web downloads as well as Mail attachments. On iOS everything is working fine, but on Android the File Association isn't working. The program is running fine, but when trying to open XML files Android says "cannot open file"

I tried different MemeTypes, DataShemes and Actions but none are working.

[Activity(Label = "MyApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTask,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new[] { Intent.ActionView},
    Categories = new[] { Intent.CategoryDefault },
    DataScheme = "file",
    DataHost = "*",
    DataPathPattern = ".*\\.xml",
    DataMimeType = @"application/xaml+xml")]

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        string action = Intent.Action;
        string type = Intent.Type;

        //If opened to recieve a file
        if (Intent.ActionView.Equals(action) && !String.IsNullOrEmpty(type))
        {
            Console.WriteLine("Opened to recieve a file!");
        }

        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();
        LoadApplication(new App());
    }

I expect the application to be used for opening & sending .xml files which will then be handled.

Edit: Link to test Application, where Console shout say "Opened to recieve a file" when it works. https://dracoon.team/#/public/shares-downloads/BMCPjb5XGlWyQArahpnl2x1uoHmBCRv6

Upvotes: 1

Views: 439

Answers (1)

Jonwd
Jonwd

Reputation: 655

For the people who are looking for a solution in how to associate your app with open with... (which I wasn't able to find a clear answer to this problem). Here is what worked for me:

I'm using Xamarin 16.7

Just like Louis (OP), I was trying the following IntentFilter:

[IntentFilter(new[] { Intent.ActionView},
    Categories = new[] { Intent.CategoryDefault },
    DataScheme = "file",
    DataHost = "*",
    DataPathPattern = ".*\\.txt",
    DataMimeType = @"*/*")]

To test the above intent filter, I would go to the default file app, and try to open a txt file, but my app wouldn't show as an option.

Then I changed it to:

[IntentFilter(new[] { Intent.ActionView }, 
Categories = new[] { Intent.CategoryDefault }, DataMimeType = @"text/plain")]

And it worked. The problem was that in DataScheme, when trying open with..., it wasn't file, it was actually content.

If you have these kind of problems, make sure the filter is setup correctly.

Upvotes: 1

Related Questions