Xamarin Android:如何从资产文件夹共享 PDF 文件?通过 WhatsApp 我收到消息说您选择的文件不是文档

Xamarin Android: How to Share PDF File From Assets Folder? Via WhatsApp I get message that the file you picked was not a document

我使用 Xamarin Android。我有一个 PDF 文件存储在 Xamarin Android 的 Assets 文件夹中。

我想在 WhatsApp 中共享此文件,但我收到消息:

The file you picked was not a document.

我尝试了两种方法:

这是第一种方式

var SendButton = FindViewById<Button>(Resource.Id.SendButton);
SendButton.Click += (s, e) =>

                {
                ////Create a new file in the exteranl storage and copy the file from assets folder to external storage folder
                Java.IO.File dstFile = new Java.IO.File(Environment.ExternalStorageDirectory.Path + "/my-pdf-File--2017.pdf");
                dstFile.CreateNewFile();

                var inputStream = new FileInputStream(Assets.OpenFd("my-pdf-File--2017.pdf").FileDescriptor);
                var outputStream = new FileOutputStream(dstFile);
                CopyFile(inputStream, outputStream);

                //to let system scan the audio file and detect it
                Intent intent = new Intent(Intent.ActionMediaScannerScanFile);
                intent.SetData(Uri.FromFile(dstFile));
                this.SendBroadcast(intent);

                //share the Uri of the file
                var sharingIntent = new Intent();
                sharingIntent.SetAction(Intent.ActionSend);
                sharingIntent.PutExtra(Intent.ExtraStream, Uri.FromFile(dstFile));
                sharingIntent.SetType("application/pdf");

                this.StartActivity(Intent.CreateChooser(sharingIntent, "@string/QuotationShare"));
            };

这是第二个

//Other way

            var SendButton2 = FindViewById<Button>(Resource.Id.SendButton2);
            SendButton2.Click += (s, e) =>
            {

                Intent intent = new Intent(Intent.ActionSend);
                intent.SetType("application/pdf");

                Uri uri = Uri.Parse(Environment.ExternalStorageDirectory.Path + "/my-pdf-File--2017.pdf");
                intent.PutExtra(Intent.ExtraStream, uri);

                try
                {
                    StartActivity(Intent.CreateChooser(intent, "Share PDF file"));
                }
                catch (System.Exception ex)
                {
                    Toast.MakeText(this, "Error: Cannot open or share created PDF report. " + ex.Message, ToastLength.Short).Show();
                }
            };

另外,当我通过电子邮件共享时,发送的 PDF 文件是空的(损坏的文件)

我能做什么?

the file you picked was not a document

当我尝试通过 WhatsApp 从资产文件夹共享 .pdf 文件时遇到了这个问题,但它给了我与你的问题相同的错误:

the file you picked was not a document

最后我得到了一个解决方案,将资产文件夹中的 .pdf 文件复制到 下载文件夹 ,它工作正常:

var pathFile = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);

Java.IO.File dstFile = new Java.IO.File(pathFile.AbsolutePath + "/my-pdf-File--2017.pdf");

效果类似this。

解决方案是将 .pdf 文件从 assets 文件夹复制到 local storage。然后我们就可以共享文件了。

首先复制文件:

string fileName = "my-pdf-File--2017.pdf";

var localFolder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var MyFilePath = System.IO.Path.Combine(localFolder, fileName);

using (var streamReader = new StreamReader(Assets.Open(fileName)))
{
       using (var memstream = new MemoryStream())
       {
              streamReader.BaseStream.CopyTo(memstream);
              var bytes = memstream.ToArray();
              //write to local storage
              System.IO.File.WriteAllBytes(MyFilePath, bytes);

              MyFilePath = $"file://{localFolder}/{fileName}";
       }
}

然后从本地存储共享文件

var fileUri = Android.Net.Uri.Parse(MyFilePath);

var intent = new Intent();
intent.SetFlags(ActivityFlags.ClearTop);
intent.SetFlags(ActivityFlags.NewTask);
intent.SetAction(Intent.ActionSend);
intent.SetType("*/*");
intent.PutExtra(Intent.ExtraStream, fileUri);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);

var chooserIntent = Intent.CreateChooser(intent, title);
chooserIntent.SetFlags(ActivityFlags.ClearTop);
chooserIntent.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(chooserIntent);