|
2 | 2 |
|
3 | 3 | using Android.App; |
4 | 4 | using Android.Content.PM; |
| 5 | +using Android.Content; |
5 | 6 | using Android.Runtime; |
6 | 7 | using Android.Views; |
7 | 8 | using Android.Widget; |
8 | 9 | using Android.OS; |
| 10 | +using System.IO; |
| 11 | +using Android.Provider; |
| 12 | +using System.Text; |
9 | 13 |
|
10 | 14 | namespace BasicPlainTextReaderApp.Droid |
11 | 15 | { |
12 | | - [Activity(Label = "Basic Plain Text Reader", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )] |
| 16 | + [Activity(Label = "Basic Plain Text Reader", |
| 17 | + Icon = "@mipmap/icon", |
| 18 | + Theme = "@style/MainTheme", |
| 19 | + MainLauncher = true, |
| 20 | + ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)] |
| 21 | + [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = @"*/*")] |
13 | 22 | public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity |
14 | 23 | { |
15 | 24 | protected override void OnCreate(Bundle savedInstanceState) |
16 | 25 | { |
| 26 | + string action = Intent.Action; |
| 27 | + string type = Intent.Type; |
| 28 | + StringBuilder sb = null; |
| 29 | + |
| 30 | + if(Intent.ActionView.Equals(action) && !string.IsNullOrEmpty(type)) |
| 31 | + { |
| 32 | + Android.Net.Uri fileUri = Intent.Data; |
| 33 | + if (fileUri != null) |
| 34 | + { |
| 35 | + try |
| 36 | + { |
| 37 | + using (var parcelFileDescriptor = ContentResolver.OpenFileDescriptor(Intent.Data, "r")) |
| 38 | + using (Java.IO.FileDescriptor fileDescriptor = parcelFileDescriptor.FileDescriptor) |
| 39 | + using (var reader = new Java.IO.FileReader(fileDescriptor)) |
| 40 | + using (var bufferedReader = new Java.IO.BufferedReader(reader)) |
| 41 | + { |
| 42 | + string line; |
| 43 | + sb = new StringBuilder(); |
| 44 | + while ((line = bufferedReader.ReadLine()) != null) |
| 45 | + { |
| 46 | + sb.AppendLine(line); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + catch (Exception e) |
| 51 | + { |
| 52 | + sb = new StringBuilder(); |
| 53 | + sb.AppendLine(e.ToString()); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
17 | 58 | TabLayoutResource = Resource.Layout.Tabbar; |
18 | 59 | ToolbarResource = Resource.Layout.Toolbar; |
19 | 60 |
|
20 | 61 | base.OnCreate(savedInstanceState); |
21 | 62 |
|
22 | 63 | Xamarin.Essentials.Platform.Init(this, savedInstanceState); |
23 | 64 | global::Xamarin.Forms.Forms.Init(this, savedInstanceState); |
24 | | - LoadApplication(new App()); |
| 65 | + LoadApplication(new App(sb.ToString())); |
25 | 66 | } |
26 | 67 | public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) |
27 | 68 | { |
|
0 commit comments