CreateChooser无法在Android v5.1中运行

时间:2017-10-22 05:08:20

标签: xamarin android-intent xamarin.android file-sharing

我正在使用意图共享pdf文件。我在共享文件时限制应用程序。我想将文件分享给Good document,Kindle和drop box。我使用下面的代码来实现这一点。但是下面的代码在android v5.1中不起作用。该设备具有所需的应用程序共享。但它正在显示"分享时,没有应用可以执行此操作" 。你有没有人建议你的想法来解决这个问题?

var pathFile = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            var m_documentMobiNames = shortName + "." + fileType;
            var mobileFileName = Path.Combine(pathFile, m_documentMobiNames);
            var shareIntentsLists = new List<Intent>();
            Intent sendIntent = new Intent();
            sendIntent.SetAction(Intent.ActionSend);
            sendIntent.SetType("application/pdf");
            var resInfos = context.PackageManager.QueryIntentActivities(sendIntent, 0);
            if (resInfos.Count > 0)
            {
                foreach (var resInfo in resInfos)
                {
                    string packageName = resInfo.ActivityInfo.PackageName;
                    if (packageName.Contains("com.google.android.apps.docs") || packageName.Contains("com.dropbox.android") || packageName.Contains("com.amazon.kindle"))
                    {
                        Intent intent = new Intent();
                        intent.SetComponent(new ComponentName(packageName, resInfo.ActivityInfo.Name));
                        intent.SetAction(Intent.ActionSend);
                        intent.SetType("application/pdf");
                        intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + mobileFileName));
                        intent.SetPackage(packageName);
                        shareIntentsLists.Add(intent);
                    }
                }
            }


            if (shareIntentsLists.Count > 0)
            {

                chooserIntent = Intent.CreateChooser(new Intent(), "Share with");
                chooserIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse(mobileFileName));
                chooserIntent.PutExtra(Intent.ExtraInitialIntents, shareIntentsLists.ToArray());
                chooserIntent.SetFlags(ActivityFlags.ClearTop);
                chooserIntent.SetFlags(ActivityFlags.NewTask);
                context.StartActivity(chooserIntent);
                await Task.FromResult(true);

            }

0 个答案:

没有答案