PDFRenderer cannot display file

I am using API level 21 and I am trying to display a PDF file downloaded from the Internet and saved 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 above code crashes the application 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 can’t explain to myself why this code doesn’t work, does anyone know how to fix it?

+5
source share
1 answer

From my experience, this is related to the file you are passing to PdfRenderer. If you can open the same file with another application / library from the same directory that you save, this means that you must open it with PdfRenderer.

The official issue here https://github.com/googlesamples/android-PdfRendererBasic/issues/1 was closed and the original reporter did not reopen it.

Can you try the exact same sample with your own pdf file? If it does not work, then there is an error with the library, and I suggest you re-open this problem with a file that does not work. You can also try other files with your solution (very simple files).

+2
source

All Articles