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 ?

1

3 Answers 3

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()) } 
Sign up to request clarification or add additional context in comments.

2 Comments

Correction in the above code is File assist=new File("/mnt/sdcard");
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.
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:

2 Comments

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...
Assuming that you have data content which is ready to be writen in PDF , See update
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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.