我工作的地方我在字节数组的形式获取数据的应用程序,我需要使用此字节数组用于创建一个新的PDF file.How我可以在android系统实现这一目标?
下面是从字节数组创建PDF的方式之一。
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())
}
您可以使用Common IO库,这将有助于转换简单地从文件到字节数组或字节数组文件。
File mFile = new File(filePath);
FileUtils.writeByteArrayToFile(mFile, yourArray);