【发布时间】:2012-01-28 11:45:49
【问题描述】:
我正在开发一个应用程序,我以字节数组的形式获取数据,我需要使用这个字节数组来创建一个新的 PDF 文件。我如何在 android 中实现这一点?
【问题讨论】:
-
看看这个:stackoverflow.com/questions/1131116/…>
我正在开发一个应用程序,我以字节数组的形式获取数据,我需要使用这个字节数组来创建一个新的 PDF 文件。我如何在 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);
【讨论】: