如何在Android中将字节数组转换为PDF文件?
我正在开发一个应用程序,我以字节数组的forms获取数据,我需要使用这个字节数组来创建一个新的PDF文件。我怎么能在android中实现这一点?
试试这个代码,它对我有用。
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("Sorry! Your given file is too large.","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);
创建的mydemo.pdf将存储在您的SD卡上。
使用FileOutputStream
及其write(byte[])
方法
#假设您已准备好以PDF格式编写数据内容
还有一些pdf编写器api可用于android
看到
- Andriod pdf作家
- 如何在android上创建pdf
您可以使用Common IO库,它有助于简单地从文件转换为字节数组或从字节数组转换为文件。
File mFile = new File(filePath); FileUtils.writeByteArrayToFile(mFile, yourArray);