找不到活动来处理Intent Android

时间:2016-09-15 10:09:20

标签: c# xamarin.android

我有一个xamarin android应用程序,我想打开一个pdf文件。在代码的最后一行后我得到了错误

  

Android.Content.ActivityNotFoundException:找不到要处理的Activity   意图{act = android.intent.action.VIEW   dat = / storage / emulated / 0 / docen __。pdf typ = application / pdf flg = 0x80000}

 Stream _IS = this.Assets.Open("doc" + GlobalsAndroid.FixedDocumentationLanguageCode + ".pdf");
                                        byte[] _BA = null;
                                        using (BinaryReader _BR = new BinaryReader(_IS))
                                        {
                                            using (MemoryStream ms = new MemoryStream())
                                            {
                                                byte[] _buffer = new byte[4096];
                                                int count;
                                                while ((count = _BR.Read(_buffer, 0, _buffer.Length)) != 0)
                                                    ms.Write(_buffer, 0, count);
                                                _BA = ms.ToArray();
                                            }
                                        }

                                        string _EP = System.IO.Path.Combine(global::Android.OS.Environment.ExternalStorageDirectory.Path, "doc" + GlobalsAndroid.FixedDocumentationLanguageCode + "__.pdf");
                                        System.IO.File.WriteAllBytes(_EP, _BA);

                                        Intent _I = new Intent(Intent.ActionView);
                                        _I.SetDataAndType(global::Android.Net.Uri.Parse(_EP), "application/pdf");
                                        _I.SetFlags(ActivityFlags.GrantReadUriPermission);
                                        _I.SetFlags(ActivityFlags.NewTask);
                                        _I.SetFlags(ActivityFlags.ClearWhenTaskReset);

                                        this.StartActivity(_I);

我的清单是:

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  android:versionCode="6" android:versionName="1.3" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="20" />
    <application   android:debuggable="true"></application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.intent.action.VIEW" />

</manifest>

我该如何解决?

1 个答案:

答案 0 :(得分:0)

经过一些更改后,它可以正常工作。

 var path = System.IO.Path.Combine(global::Android.OS.Environment.ExternalStorageDirectory.Path, "doc" + GlobalsAndroid.FixedDocumentationLanguageCode + ".pdf");

                                        var intent = new Intent(Intent.ActionView);
                                        global::Android.Net.Uri pdfFile = global::Android.Net.Uri.FromFile(new Java.IO.File(path));
                                        intent.SetDataAndType(pdfFile, "application/pdf");
                                        intent.SetFlags(ActivityFlags.GrantReadUriPermission);
                                        intent.SetFlags(ActivityFlags.NewTask);
                                        intent.SetFlags(ActivityFlags.ClearWhenTaskReset);

                                        this.StartActivity(intent);