Skip to content

Instantly share code, notes, and snippets.

@brendanzagaeski
Last active November 25, 2023 22:07
Show Gist options
  • Save brendanzagaeski/70afe08654927befb26b to your computer and use it in GitHub Desktop.
Save brendanzagaeski/70afe08654927befb26b to your computer and use it in GitHub Desktop.
Tell Android to open a particular file type in your Xamarin.Android app
using Android.App;
using Android.Content;
// Tell Android to open a particular file type in your Xamarin.Android app
// Different apps will produce different intents for the same file, so
// you will need multiple intent filters to handle all the cases.
// You can look in the `adb logcat` logs to see which intent appears
// after you have tapped on a file in an app.
namespace AndroidApplication1
{
[Activity(Label = "FooFileActivity")]
//
// Example Intent for link in web browser:
// START u0 {act=android.intent.action.VIEW dat=http://www.example.com/test.foo typ=application/octet-stream}
//
// Example Intent for link in e-mail:
// START u0 {act=android.intent.action.VIEW dat=http://www.example.com/test.foo flg=0x90000}
//
[IntentFilter(new string[] { Intent.ActionView },
Categories = new string[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "http",
DataHost = "*",
// Double-escape the backslashes in C# so they end up
// single-escaped in the generated manifest file
// `obj/Debug/android/AndroidManifest.xml`
DataPathPattern = ".*\\\\.foo"
)]
//
// Example Intent from tapping attachment in Email app
// START u0 {act=android.intent.action.VIEW dat=content://com.android.email.attachmentprovider/1/1/RAW typ=application/foo flg=0x80001}
//
[IntentFilter(new string[] { Intent.ActionView },
Categories = new string[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "content",
DataHost = "*",
DataMimeType = "application/foo"
)]
public class FooFileActivity : Activity
{
}
[Activity(Label = "DownloadsGmailAppsFileActivity")]
//
// Example intent from tapping file in Downloads app
// START u0 {act=android.intent.action.VIEW dat=content://downloads/all_downloads/1 typ=application/octet-stream flg=0x3}
//
// Example Intent from tapping attachment in Gmail app
// START {act=android.intent.action.VIEW dat=content://gmail-ls/[email protected]/messages/4/attachments/0.1/BEST/false typ=application/octet-stream flg=0x80001 u=0}
//
// This IntentFilter isn't so useful because it will match _any_ file
// that has the "application/octet-stream" MIME type, not just `foo`
// files.
//
[IntentFilter(new string[] { Intent.ActionView },
Categories = new string[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "content",
DataHost = "*",
DataMimeType = "application/octet-stream"
)]
public class DownloadsGmailAppsFileActivity : Activity
{
}
}
@saamerm
Copy link

saamerm commented Sep 19, 2017

Thanks for the help! Worked well for reference!

@jgold6
Copy link

jgold6 commented Aug 15, 2018

IN the first intent filter that has

DataPathPattern = ".*\\\\.foo"

I believe that is incorrect as you are escaping the backslash twice which if reading this correctly will yield:

\<anysinglecharacter>foo

To escape the ., match any path that ends with .foo I think this would be the correct format:

DataPathPattern = ".*\\.foo"

@rajaei
Copy link

rajaei commented Aug 30, 2021

worked for me thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment