1111using Android . Provider ;
1212using System . Text ;
1313using BasicPlainTextReaderApp . Library ;
14+ using Java . Interop ;
15+ using Android . Util ;
16+ using Java . IO ;
1417
1518namespace BasicPlainTextReaderApp . Droid
1619{
@@ -24,36 +27,30 @@ public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompa
2427 {
2528 protected override void OnCreate ( Bundle savedInstanceState )
2629 {
27- string action = Intent . Action ;
28- string type = Intent . Type ;
29-
3030 TextModel data = null ;
31-
32- if ( Intent . ActionView . Equals ( action ) && ! string . IsNullOrEmpty ( type ) )
31+
32+ if ( Intent . ActionView . Equals ( Intent . Action ) && Intent . Data != null && ! string . IsNullOrEmpty ( Intent . Type ) )
3333 {
34- if ( Intent . Data != null )
34+ try
3535 {
36- try
36+ using ( var parcelFileDescriptor = ContentResolver . OpenFileDescriptor ( Intent . Data , "r" ) )
37+ using ( Java . IO . FileDescriptor fileDescriptor = parcelFileDescriptor . FileDescriptor )
38+ using ( var reader = new Java . IO . FileReader ( fileDescriptor ) )
39+ using ( var bufferedReader = new Java . IO . BufferedReader ( reader ) )
3740 {
38- using ( var parcelFileDescriptor = ContentResolver . OpenFileDescriptor ( Intent . Data , "r" ) )
39- using ( Java . IO . FileDescriptor fileDescriptor = parcelFileDescriptor . FileDescriptor )
40- using ( var reader = new Java . IO . FileReader ( fileDescriptor ) )
41- using ( var bufferedReader = new Java . IO . BufferedReader ( reader ) )
41+ string line ;
42+ var sb = new StringBuilder ( ) ;
43+ while ( ( line = bufferedReader . ReadLine ( ) ) != null )
4244 {
43- string line ;
44- var sb = new StringBuilder ( ) ;
45- while ( ( line = bufferedReader . ReadLine ( ) ) != null )
46- {
47- sb . AppendLine ( line ) ;
48- }
49- data = new TextModel ( sb . ToString ( ) , Intent . DataString , Intent . Type , Intent . Data . Path ) ;
45+ sb . AppendLine ( line ) ;
5046 }
51- }
52- catch ( Exception e )
53- {
54- data = new TextModel ( e . ToString ( ) , Intent . DataString , Intent . Type , Intent . Data . Path ) ;
47+ data = new TextModel ( sb . ToString ( ) , Intent . DataString , Intent . Type , Intent . Data . Path ) ;
5548 }
5649 }
50+ catch ( Exception e )
51+ {
52+ data = new TextModel ( e . ToString ( ) , Intent . DataString , Intent . Type , Intent . Data . Path ) ;
53+ }
5754 }
5855
5956 TabLayoutResource = Resource . Layout . Tabbar ;
@@ -71,5 +68,36 @@ public override void OnRequestPermissionsResult(int requestCode, string[] permis
7168
7269 base . OnRequestPermissionsResult ( requestCode , permissions , grantResults ) ;
7370 }
71+
72+ [ Export ( "OpenTextFile" ) ]
73+ public void OpenTextFile ( )
74+ {
75+ var backingFile = Path . Combine ( System . Environment . GetFolderPath ( System . Environment . SpecialFolder . Personal ) , "test.txt" ) ;
76+ if ( ! System . IO . File . Exists ( backingFile ) )
77+ {
78+ using ( var writer = System . IO . File . CreateText ( backingFile ) )
79+ {
80+ writer . WriteLine ( "Test file\r \n Test file line 2" ) ;
81+ writer . Flush ( ) ;
82+ }
83+ }
84+
85+ //Read file
86+ //using (var reader = new StreamReader(backingFile, true))
87+ //{
88+ // string line;
89+ // while ((line = reader.ReadLine()) != null)
90+ // {
91+ // }
92+ //}
93+
94+ var uri = Android . Net . Uri . Parse ( "file:///data/user/0/dev.jonwolf.basicplaintextreaderapp/files/test.txt" ) ;
95+ var intent2 = new Intent ( Intent . ActionView , uri , this , typeof ( MainActivity ) ) ;
96+
97+ intent2 . SetDataAndType ( uri , "text/plain" ) ;
98+ intent2 . SetAction ( Intent . ActionView ) ;
99+
100+ StartActivity ( intent2 ) ;
101+ }
74102 }
75103}
0 commit comments