22using Android . Content ;
33using Android . Content . PM ;
44using Android . OS ;
5+ using Android . Runtime ;
56using Android . Util ;
67using Avalonia ;
78using Avalonia . Android ;
@@ -25,6 +26,7 @@ namespace Vocup.Android;
2526public class MainActivity : AvaloniaMainActivity < App >
2627{
2728 private const string Tag = "Vocup.Android.MainActivity" ;
29+ private const int FilePickerRequestCode = 3147 ;
2830
2931 protected override AppBuilder CreateAppBuilder ( )
3032 {
@@ -50,6 +52,38 @@ protected override void OnNewIntent(Intent? intent)
5052 HandleIntent ( intent ) ;
5153 }
5254
55+ public void ShowFilePicker ( )
56+ {
57+ var intent = new Intent ( Intent . ActionGetContent ) ;
58+ intent . SetType ( "application/octet-stream" ) ;
59+ intent . AddCategory ( Intent . CategoryOpenable ) ;
60+ intent . AddFlags ( ActivityFlags . GrantReadUriPermission ) ;
61+ StartActivityForResult ( Intent . CreateChooser ( intent , "Select a file" ) , FilePickerRequestCode ) ;
62+ }
63+
64+ protected override void OnActivityResult ( int requestCode , [ GeneratedEnum ] Result resultCode , Intent ? data )
65+ {
66+ base . OnActivityResult ( requestCode , resultCode , data ) ;
67+
68+ if ( requestCode == FilePickerRequestCode && resultCode == Result . Ok && data ? . Data != null )
69+ {
70+ using System . IO . Stream ? stream = ContentResolver ? . OpenInputStream ( data . Data ) ;
71+
72+ if ( stream == null )
73+ {
74+ Log . Error ( Tag , "Stream is null in OnActivityResult" ) ;
75+ return ;
76+ }
77+ if ( Avalonia . Application . Current is not App app )
78+ {
79+ Log . Error ( Tag , "App is null in OnActivityResult" ) ;
80+ return ;
81+ }
82+ Log . Debug ( Tag , $ "File size is { stream . Length } bytes") ;
83+ app . OpenFile ( stream ) ;
84+ }
85+ }
86+
5387 private void HandleIntent ( Intent ? intent )
5488 {
5589 if ( intent ? . Action == Intent . ActionView && intent . Data != null )
0 commit comments