AndroidPdfViewer
AndroidPdfViewer copied to clipboard
java.io.IOException: cannot create document: File not in PDF format or corrupted.
java.io.IOException: cannot create document: File not in PDF format or corrupted.
this is my code 👍 private void displayFromFile(String path) { pdfFileName = getFileName(path); File file = new File(path); if (file.exists()) { pdfView.fromFile(file) .defaultPage(pageNumber) .onPageChange(this) .enableAnnotationRendering(true) .onLoad(this) .onError(this) .scrollHandle(new DefaultScrollHandle(context)) .load(); } }
I download the pdf from pdfUrl = "http://7xwqut.com2.z0.glb.qiniucdn.com/20160928191134_video.pdf";
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"video.pdf");
Did you check if file is not corrupted, e.g. opening it in another pdf reader?
I can open the pdf in windows ,and i also can open this pdf in tencent QQ
You should try opening file on your device, not on PC. File may be corrupted after transferring to Android device.
yes I also can open it with http://www.jisupdf.com/ this apk on my device
this is the local path: /storage/emulated/0/Download/video.pdf the file is exist
If file would not exist you would get FileNotFoundException. Current error means that library is not able to open this file. But, as I wrote in your previous issue, I can open this file on every of my 4 Android devices.
my device is android 4.4.4 ktu84p MI 4c
hey, I think Im having this very same issue with one of my pdf files. have u been able to resolve it?
have u been able to resolve it?
i am facing same issue.
same problem.How to resolve?
Maybe kind of a late reply... Had the same error. For some reason reading PDF directly from assets did not work, and gave the above mentioned error.
So copied it from asset to cache dir, and then all worked. No idea why. Since I copied it from asset, the file in asset cannot be corrupted, but the below worked, so doing that.
Below code found on github:
// In this sample, we read a PDF from the assets directory.
File file = new File(getContext().getCacheDir(), FILENAME);
if (!file.exists()) {
InputStream asset = null;
try {
asset = getContext().getAssets().open(FILE);
FileOutputStream output = null;
output = new FileOutputStream(file);
final byte[] buffer = new byte[1024];
int size;
while ((size = asset.read(buffer)) != -1) {
output.write(buffer, 0, size);
}
asset.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
if (fileDescriptor != null) {
pdfRenderer = new PdfRenderer(fileDescriptor);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Same problem, any update about the issue?
Same problem, any update?
I had the same error but resolved. The problem was in program logic. When I was creating the PDF file (writing to disk essentially rather than downloading to the disk), it was writing wrong amount of data to the disk, hence corrupted file.
while((count = inputStream.read(data)) != -1)
{
total += count;
publishProgress((int) total * 100/contentLength);
fileOutputStream.write(data, 0,count);
}
In here my mistake was count += count, when I fixed that file could be opened successfully. Rest of the program started working fine too.
也许有点迟到了。。。 有同样的错误。由于某种原因,直接从资产中读取PDF无效,并且出现了上述错误。
因此,将其从资产复制到缓存目录,然后一切正常。不知道为什么。由于我是从资产中复制文件的,因此资产中的文件不会被破坏,但是下面的方法可以正常工作,因此可以这样做。
在github上找到以下代码:
// In this sample, we read a PDF from the assets directory. File file = new File(getContext().getCacheDir(), FILENAME); if (!file.exists()) { InputStream asset = null; try { asset = getContext().getAssets().open(FILE); FileOutputStream output = null; output = new FileOutputStream(file); final byte[] buffer = new byte[1024]; int size; while ((size = asset.read(buffer)) != -1) { output.write(buffer, 0, size); } asset.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } } try { fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); if (fileDescriptor != null) { pdfRenderer = new PdfRenderer(fileDescriptor); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
Your answer helped me. Thank you.
Same problem, any update?