Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have .net web service which returning PDF file as byte[]

C#
[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;
}



I know how to call web service from android. my question is how to display this byte[] as PDF in android application?
Posted
Updated 16-Feb-17 19:59pm

1 solution

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
// 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);
}


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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900