Last active
November 25, 2023 22:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IN the first intent filter that has
I believe that is incorrect as you are escaping the backslash twice which if reading this correctly will yield:
To escape the
.
, match any path that ends with.foo
I think this would be the correct format: