Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/user@gmail.com/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

This comment has been minimized.

Copy link

@saamerm saamerm commented Sep 19, 2017

Thanks for the help! Worked well for reference!

@jgold6

This comment has been minimized.

Copy link

@jgold6 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.