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.

Aswin P Ashok
  • 702
  • 8
  • 27

0 Answers0