How to convert byte array to PDF file in Android?… here is a solution to the problem.
How to convert byte array to PDF file in Android?
I’m developing an application that gets data as a byte array that I need to use to create a new PDF file. How do I implement this in Android?
Solution
This is one of the methods to create a PDF from a 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())
}