我正在编写 Android 应用程序并尝试将 PDF 文件转换为图像。
我正在使用图书馆 pdfviewerlibrary
.这是我的代码的开头:
File f = new File(Environment.getExternalStorageDirectory().getPath()+"/manual.pdf");
long len = f.length();
RandomAccessFile raf = new RandomAccessFile(f, "r");
FileChannel channel = raf.getChannel();
ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()));
PDFFile mPdfFile = new PDFFile(bb);
问题是,当我创建新的 PDFFile
时,它会抛出异常
java.io.exception this may not be a PDF file.
显然 manual.pdf
是一个 PDF 文件,但是当我检查 length
时它显示为 0,我知道它不应该...
我不知道该怎么办,有人遇到过同样的问题吗??
最佳答案
要解决此问题,请删除文件名前的斜杠。代码将是:
File f = new File(Environment.getExternalStorageDirectory().getPath()+"manual.pdf");
long len = f.length();
RandomAccessFile raf = new RandomAccessFile(f, "r");
FileChannel channel = raf.getChannel();
ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()));
PDFFile mPdfFile = new PDFFile(bb);
关于android - java.io.exception 这可能不是 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22126631/