Hugaro
Hugaro

Reputation: 53

java.io.exception this may not be a PDF file

I'm coding an Android app and trying to convert a PDF file to an image. I'm using the library pdfviewerlibrary. This is the beginning of my code:

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);

The problem is that, when I create the new PDFFile, it throws the exception

java.io.exception this may not be a PDF file.

Obviously manual.pdf is a PDF file, but when I check the length it says 0, and I know it shouldn't...

I don't know what to do, anyone already had the same problem??

Upvotes: 1

Views: 706

Answers (1)

Khalid Taha
Khalid Taha

Reputation: 3313

To resolve the problem, remove the slash before the file name. The code will be:

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);

Upvotes: 1

Related Questions