Register your app to open files
Android allows an app to register as a file or protocol handler. This guide shows how to register an Avalonia app to handle text files and how to receive them via Avalonia's storage framework.
-
Add an
IntentFilterattribute to your activity. This attribute automatically registers the intent filter in the Android manifest during build. -
Attach a listener to the
IAvaloniaActivity.Activatedevent. When Android activates your app to open a file, this event will be raised withFileActivatedEventArgs. -
Forward the storage items from the event data to your view model. You can use the static
Avalonia.Application.Currentproperty to access yourAppinstance from an activity. In yourAppclass you can store your view model instance in a field after creating it inOnFrameworkInitializationCompleted.
[Activity(
Label = "Demo.Android",
Theme = "@style/MyTheme.NoActionBar",
Icon = "@drawable/icon",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.UiMode)]
[IntentFilter(["android.intent.action.VIEW"],
Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable],
DataSchemes = ["file", "content"],
DataMimeType = "text/plain",
DataPathPattern = ".*\\.txt")]
public class MainActivity : AvaloniaMainActivity<App>
{
public MainActivity()
{
((IAvaloniaActivity)this).Activated += HandleIntent;
}
private static void HandleIntent(object? sender, ActivatedEventArgs e)
{
if (e is FileActivatedEventArgs fileActivated && Avalonia.Application.Current is App app)
{
app.OpenFiles(fileActivated.Files);
}
}
}
public partial class App : Application
{
private MainViewModel? mainViewModel;
public void OpenFiles(IReadOnlyList<IStorageItem> files)
{
mainViewModel?.OpenFiles(files);
}
}
public class MainViewModel
{
public async void OpenFiles(IReadOnlyList<IStorageItem> files)
{
foreach (IStorageItem item in files)
{
if (item is IStorageFile file)
{
using Stream stream = await file.OpenReadAsync();
// Read the stream (use StreamReader, etc.)
}
}
}
}
Have questions or feedback? Join the conversation below.