How to convert byte array to PDF file in Android?

I am working on an application in which I receive data in the form of a byte array, and I need to use this byte array to create a new PDF file. How can i achieve this in android?

+4
source share
3 answers

File dir = Environment.getExternalStorageDirectory (); File Assistant = 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()) } 
+2
source

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

# Assuming you have data content that is ready to be written to PDF

There are also some api pdf writers available for android

Cm

+1
source

You can use the Common IO library, which will help to convert simply from a file to a byte array or from an array of bytes to a file.

  File mFile = new File(filePath); FileUtils.writeByteArrayToFile(mFile, yourArray); 
0
source

Source: https://habr.com/ru/post/1388120/


All Articles