在Android应用程序中显示PDF文档 [英] Display PDF document in android application

查看:61
本文介绍了在Android应用程序中显示PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有.net网络服务,将PDF文件返回为 byte []



I have .net web service which returning PDF file as byte[]

[WebMethod]
    public byte[] GetFile(string filename)

{
        BinaryReader binReader = new BinaryReader(File.Open(Server.MapPath(filename), FileMode.Open, FileAccess.Read));
        binReader.BaseStream.Position = 0;
        byte[] binFile = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length));
        binReader.Close();
        return binFile;
}







我知道如何从android调用web服务。我的问题是如何在Android应用程序中将此字节[]显示为PDF?




I know how to call web service from android. my question is how to display this byte[] as PDF in android application?

推荐答案

我知道的最简单的解决方案,如果已经安装了PDF查看器,将你的字节数组保存到文件并请求显示文件。



这样的东西...



The simplest solution I'm aware of, if there's a PDF viewer already installed, would be to save your byte array to file and request display of the file.

Something like this...

// Java. If you're working with a C# 'droid tool you may need to tinker with the
//       syntax.

// filePath the name and directory where the byte array was saved.
File pdfFile = new File(filePath);

if (pdfFile.exists()) {
  Uri path = Uri.fromFile(pdfFile);
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(path, "application/pdf");
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  // If requesting from a fragment...
  getActivity().startActivity(intent);
  // If requesting from an activity...
  // this.startActivity(intent);
}





当然,当PDF查看器活动完成时,您需要整理文件。可以使用 startActivityForResult 而不是 startActivity 来执行此操作。



Of course you then need to tidy away the file when the PDF viewer activity is done. It may be possible to do this using startActivityForResult instead of startActivity.


这篇关于在Android应用程序中显示PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆