Search code examples
androidxamarinintentfilter

Xamarin Android How to declare Intent Filter correctly to match custom file types?


I am currently working on an app which features should include a custom file extension to which my app is connected. What I basically want is that when I click the custom file in the Android file system my app starts automatically and reads the file contents. To achieve this I tried to implement an Intent Filter, but I just can't get it to work.

How my IntentFilter looks:

[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataMimeType ="*/*" ,DataSchemes = new[] { "file","content" }, DataPathPattern = ".*\\.cm")]

My problem is that this filter does recognize .cm files but also every other file which is not what I want. I tried many different solutions which didn't match every file type but also not files of type .cm. Also I read that DataHost has to be specified or otherwise DataPathPattern will be ignored, but when I also include DataHost in the declaration, nothing works anymore.

Any help will be appreciated, thanks!


Solution

  • Do you want to achieve the result like following result? enter image description here

    If so, please change your IntentFilter like following format.

        [IntentFilter(new[] { Android.Content.Intent.ActionView, Android.Content.Intent.ActionEdit }, Categories = new[] { Android.Content.Intent.CategoryDefault }, DataMimeType = "application/octet-stream", DataHost = "*", DataPathPattern = ".*\\.cm")]
    
    

    You can google:Registering Your Android App for File Types and Email Attachments, It is an helpful article about it