简体   繁体   English

如何从Xamarin.Android中的Assets文件夹中打开pdf

[英]How Do I Open pdf from Assets folder in Xamarin.Android

I want to Open a PDF in any PDFviewer and my pdf file is placed in assets folder. 我想在任何PDFviewer中打开PDF,我的pdf文件放置在资产文件夹中。

I tried doing following but File Provider also plays a part since targetversion>24. 我尝试执行以下操作,但由于targetversion> 24,文件提供程序也发挥了作用。 I have also implemented a FileProvider in Manifest file and a filepath file in xml folder under resources. 我还实现了Manifest文件中的FileProvider和资源下xml文件夹中的filepath文件。 Please help. 请帮忙。

 string fileName = "myProfile.pdf";

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

 using (var streamReader = new StreamReader(_activity.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);
 Intent intent = new Intent(Intent.ActionView);
 intent.SetDataAndType(fileUri, "application/pdf");
 intent.SetFlags(ActivityFlags.ClearTop);
 intent.SetFlags(ActivityFlags.NewTask);
 try
 {
      _activity.StartActivity(intent);
 }
 catch (ActivityNotFoundException ex)
 {
      Toast.MakeText(_activity, "NO Pdf Viewer", ToastLength.Short).Show();
 }

This is how I use intent chooser to do the above: 这就是我使用意图选择器执行上述操作的方式:

Get the stream from the assets folder using below: 使用以下方法从资产文件夹获取流:

      public Stream GetFromAssets(Context context, string assetName)
    {
        AssetManager assetManager = context.Assets;
        Stream inputStream;
        try
        {
            using (inputStream = assetManager.Open(assetName))
            {
                return inputStream;
            }

        }
        catch (Exception e)
        {
            return null;
        }
    }

Add the following helper method to for byte[] conversion: 将以下帮助方法添加到byte []转换中:

    public byte[] ReadFully(Stream input)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            input.CopyTo(ms);
            return ms.ToArray();
        }

Then update the Open pdf method as follows and it should work: 然后按如下所示更新Open pdf方法,它应该可以工作:

    private void OpenPDF(Stream inputStream)
    {
        var bytearray = ReadFully(inputStream);
        Java.IO.File file = (Java.IO.File)Java.IO.File.FromArray(bytearray);
        var target = new Intent(Intent.ActionView);
        target.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf");
        target.SetFlags(ActivityFlags.NoHistory);

        Intent intent = Intent.CreateChooser(target, "Open File");
        try
        {
            this.StartActivity(intent);
        }
        catch (ActivityNotFoundException ex)
        {
            // Instruct the user to install a PDF reader here, or something
        }
    }

Where I pass the string path of my pdf file to come up with a Java.IO.File 我在其中传递pdf文件的字符串路径以提供Java.IO.File的地方

Basically, add the following using statements: 基本上,添加以下using语句:

using Java.IO;
using Android.Content;
using Android.Net;

Revert in case of queries 在查询时还原

Goodluck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM