Android PdfRenderer throws IOException
Asked Answered
R

0

6

My application lists PDF files, and when the user selects a PDF, the app opens it. If the user selects a corrupted PDF file, PdfRenderer throws IOException (which is fine, since I catch that exception and inform the user that the file is corrupted).

But the issue is, after this happens, IOException is thrown for all the PDF files the user is trying to open (Even for the non-corrupted ones)

Relevant code

File file = new File(filePath);
mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
if (mFileDescriptor != null) {
     mPdfRenderer = new PdfRenderer(mFileDescriptor);
}

Stacktrace

openRenderer: java.io.IOException: file not in PDF format or corrupted
    at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)
    at android.graphics.pdf.PdfRenderer.<init>(PdfRenderer.java:166)
    at ****.****.****.PdfUtil.openRenderer(PdfUtil.java:63)
    at ****.****.****.PdfUtil.getMaxPages(PdfUtil.java:46)
    at ****.****.****.PdfViewActivity.init(PdfViewActivity.java:166)
    at ****.****.****.ui.PdfViewActivity.onCreate(PdfViewActivity.java:58)
    at android.app.Activity.performCreate(Activity.java:7023)
    at android.app.Activity.performCreate(Activity.java:7014)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:172)
    at android.app.ActivityThread.main(ActivityThread.java:6590)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Why is this happening? How can I fix this?

NB : I have already tried copying pdf file to cache directory. It doesn't make a difference.

Same issue in PdfRenderer google sample repo here.

Reichert answered 25/12, 2018 at 12:58 Comment(4)
Just commenting that I’m running into the exact same issue. It also appears we’re not the only ones running into this issue. I found this issue (but closed). I may attempt to add my own project and re-file this bug with my own projectWoosley
@Woosley This issue says it's been fixed in Android P.Reichert
Did you find any solution ? If yes, please share the answer.Geary
@KPradeepKumarReddy Unfortunately, no. The bug is still there.Reichert

© 2022 - 2024 — McMap. All rights reserved.