android - 如何在android中读取块文件并显示为pdf?
问题描述
是否可以将 pdf 文件转换为一些块,然后读取这些块并在 PdfViewer android 中显示这些块?如果可能,请指导我如何做到这一点。
我找到了将pdf转换为块的这种方法:
private void getBytesFromFile(File file) throws IOException {
FileInputStream is = new FileInputStream(file); //videorecorder stores video to file
java.nio.channels.FileChannel fc = is.getChannel();
java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10000);
int chunkCount = 0;
byte[] bytes;
while(fc.read(bb) >= 0){
bb.flip();
//save the part of the file into a chunk
bytes = bb.array();
storeByteArrayToFile(bytes, mRecordingFile + "." + chunkCount);//mRecordingFile is the (String)path to file
chunkCount++;
bb.clear();
}
}
private void storeByteArrayToFile(byte[] bytesToSave, String path) throws IOException {
FileOutputStream fOut = new FileOutputStream(path);
try {
fOut.write(bytesToSave);
}
catch (Exception ex) {
Log.e("ERROR", ex.getMessage());
}
finally {
fOut.close();
}
}
我使用AndroidPdfViewer来显示 pdf,但如果您知道其他显示 pdf 的库或方法,请提及。谢谢。
解决方案
您可以借助 PDF 线性化来实现这一点。基本上,这是 PDF 中的一个标准功能,其中数据以这种方式组织,以实现内容的按需流式传输。为此,您还需要使用支持线性化的 PDF 查看器(例如https://www.pdftron.com/documentation/android/guides/viewer/view-online-pdfs/)
推荐阅读
- c# - 我可以在 asp mvc core 2.0 中添加第四个路由令牌吗?
- c# - 泛型构造要求类型“Cell<'T>”是非托管类型
- c# - 调用抛出 InvalidOperationException
- python-3.x - 在 django2 中,如何分别为新对象和旧对象设置不同的默认值?
- d3.js - 传单底图上的静态 svg。d3 js json的投影
- python - 无法从 django 管理面板创建新帖子,但它是从 python shell 创建的
- javascript - 如何在网页上显示来自 Twitter 的标签计数
- r - bookdown中章节标题和文本的奇怪拉伸
- html - 应该是带有徽标的无限滚动,但不是重复第一个图像,而是从头开始
- php - PHP cURL 超链接问题