4

I'm working on an application where i am getting the data in the form of byte array ,and i need to use this byte array for creating a new PDF file.How can i achieve this in android ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Taruni
  • 1,671
  • 4
  • 22
  • 43

3 Answers3

2

Use FileOutputStream and its write(byte[]) method.

# Assuming that you have data content which is ready to be writen in PDF.

There are also some pdf writers api available for android.

See:

GoozMeister
  • 334
  • 2
  • 17
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Hi Jigar, I tried this code FileOutputStream fileos=new FileOutputStream("Hello.pdf"); but when I am trying to open this file,I am getting an error saying that Adobe Reader could not open Hello.pdf because it is either not supported file type or because the file has been damaged... – Taruni Dec 27 '11 at 12:16
  • _Assuming that you have data content which is ready to be writen in PDF_ , See update – jmj Dec 27 '11 at 12:21
2

Here is one of the way of creating PDF from Byte Array.

File dir = Environment.getExternalStorageDirectory();
File assist = new File("/mnt/sdcard/Sample.pdf");
try {
  InputStream fis = new FileInputStream(assist);

  long length = assist.length();
  if (length > Integer.MAX_VALUE) {
    Log.e("MainActivity", "cannnottt   readddd");
  }
  byte[] bytes = new byte[(int) length];
  int offset = 0;
  int numRead = 0;
  while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) {
    offset += numRead;
  }

  File data = new File(dir, "mydemo.pdf");
  OutputStream op = new FileOutputStream(data);
  op.write(bytes);
} catch (Exception ex) {
  Log.e("MainActivity", "" + ex.getMessage())
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Sunshine
  • 387
  • 3
  • 9
  • Correction in the above code is File assist=new File("/mnt/sdcard"); – Taruni Dec 28 '11 at 06:40
  • When i am trying to open this file i am an error saying that Adobe reader couldnt open the file because it is either not supported file type or because the file has been damaged. – Taruni Dec 28 '11 at 06:45
0

You can use the Common IO library which will help convert simply from file to byte array or from byte array to file.

   File mFile = new File(filePath);
  FileUtils.writeByteArrayToFile(mFile, yourArray);
Abdqr92
  • 13
  • 3