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 ?
3 Answers
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()) } 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
Taruni
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...
Jigar Joshi
Assuming that you have data content which is ready to be writen in PDF , See update
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);