【问题标题】:Download PDF as byte stream then open in default Android application in Xamarin.Forms将 PDF 下载为字节流,然后在 Xamarin.Forms 的默认 Android 应用程序中打开
【发布时间】:2018-02-22 22:09:22
【问题描述】:

我正在使用 post call 来获取包含 PDF 的所有数据的字节流,然后我想使用 Android 中的默认程序打开 PDF。稍后会为 iOS 做。

这是我的代码:

        async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        Publication p = (Publication)e.SelectedItem;
        Debug.WriteLine(p);
        if (p.folderID.Equals("-1"))
        {
            using (Stream respStream = await post(p.docNum))
            {
                byte[] buffer = new byte[respStream.Length];
                respStream.Read(buffer, 0, buffer.Length);
                string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                File.WriteAllBytes(path + "foo.pdf", buffer);
                Device.OpenUri(new Uri(path + "foo.pdf"));
            }
        }
        else
        {
            await Navigation.PushAsync(new PublicationsPage(p.folderID));
        }
    }

    private async Task<Stream> post(string id)
    {
        Dictionary<string, string> dir = new Dictionary<string, string>();
        dir.Add("LoginID", App.user.login_id);
        dir.Add("docID", id);
        var jsonReq = JsonConvert.SerializeObject(dir);
        Debug.WriteLine("req: " + (String)jsonReq);
        var content = new StringContent(jsonReq, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(url, content);
        var responseStream = await response.Content.ReadAsStreamAsync();
        return responseStream;
    }

我现在将 pdf 作为字节流下载,然后弹出一个窗口然后关闭。我该怎么做才能解决?我宁愿不为任何软件包付费,理想情况下我希望它提示要打开什么程序。

【问题讨论】:

    标签: c# android json xamarin io


    【解决方案1】:

    Ios and Android 的文件系统不同。所以,你需要使用DependencyService在不同平台上保存和加载PDF文件。

    感谢@B.6242,在这个issue中,@B.6242已经在Android和Ios中实现了DependencyService,大家可以参考一下。

    Here is an issue 关于如何在不同平台上使用文件系统。

    【讨论】:

      【解决方案2】:

      通过以下方式让它工作:https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

      在上面的代码中,将 OnItemSelected 更改为此,其中 PDFViewPage 使用上面链接中描述的 customWebView:

              async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
          {
              Publication p = (Publication)e.SelectedItem;
              Debug.WriteLine(p);
              if (p.folderID.Equals("-1"))
              {
                  using (Stream respStream = await post(p.docNum))
                  {
                      byte[] buffer = new byte[respStream.Length];
                      respStream.Read(buffer, 0, buffer.Length);
                      string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                      File.WriteAllBytes(path + "foo.pdf", buffer);
                      await Navigation.PushAsync(new PDFViewPage(path + "foo.pdf"));
                      //Device.OpenUri(new Uri(path + "foo.pdf"));
                  }
              }
              else
              {
                  await Navigation.PushAsync(new PublicationsPage(p.folderID));
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2017-10-26
        • 1970-01-01
        • 2018-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-04
        • 1970-01-01
        • 2013-07-02
        相关资源
        最近更新 更多