Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFRenderer unable to display a file

I'm using API Level 21 and I'm trying to display a PDF downloaded from internet and stored in the cache directory.

   private void openRenderer(Context context) throws IOException {
    Log.d("NoiPA", "Opening PDF File: " + pdfPath);
    // In this sample, we read a PDF from the assets directory.
    mFileDescriptor = ParcelFileDescriptor.open(new File(pdfPath),ParcelFileDescriptor.MODE_READ_ONLY);
    //mFileDescriptor = context.getAssets().openFd(pdfPath).getParcelFileDescriptor();
    // This is the PdfRenderer we use to render the PDF.
    mPdfRenderer = new PdfRenderer(mFileDescriptor);
}

The code above makes the app crash with the following stack trace:

Download: Completed -> Showing PDF
onAttach -> pdfPath: /data/data/com.vitobellini.noipa/cache/MAG 2016.pdf
Opening PDF File: /data/data/com.vitobellini.noipa/cache/MAG 2016.pdf
java.io.IOException: not create document. Error:
     at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)
     at android.graphics.pdf.PdfRenderer.<init>(PdfRenderer.java:149)
     at com.vitobellini.noipa.PdfRendererFragment.openRenderer(PdfRendererFragment.java:151)
     at com.vitobellini.noipa.PdfRendererFragment.onAttach(PdfRendererFragment.java:115)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
     at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:779)
     at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:819)
     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:660)
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5221)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I cannot explain to myself why that code is not working, anyone knows how to fix it?

like image 962
Progeny Avatar asked Oct 30 '22 02:10

Progeny


1 Answers

From my experience it has to do with file that you are passing to the PdfRenderer. If you are able to open that same file with other app/library from exactly same directory that you are saving that means you should be able to open it with PdfRenderer.

Official issue here https://github.com/googlesamples/android-PdfRendererBasic/issues/1 was closed, and original reporter hasn't reopened it.

Can you try exactly same sample with your own PDF file? If it doesn't work, than there is a bug with library, and I would suggest you to reopen that issue with file that doesn't work. Also feel free to try other files with your solution(very simple files).

like image 84
Roger Alien Avatar answered Nov 15 '22 07:11

Roger Alien