无法使用pdfRenderer从存储中呈现PDF

时间:2015-08-24 18:08:33

标签: android pdf

开始使用此处提供的示例..

https://github.com/googlesamples/android-PdfRendererBasic

它正在从assets文件夹中打开文件。来自assets文件夹的pdf呈现得很好。但是,当它尝试使用File类从我的设备加载文件(我正在使用我的android)然后我收到错误

这很好用

/**
 * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources.
 */
private void openRenderer(Context context) throws IOException {
    // In this sample, we read a PDF from the assets directory.
    mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();
    // This is the PdfRenderer we use to render the PDF.
    mPdfRenderer = new PdfRenderer(mFileDescriptor);
}

这不起作用

/**
 * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources.
 */
private void openRenderer(Context context) throws IOException {
    // In this sample, we read a PDF from the assets directory.
    File file=new File("/storage/emulated/0/Download/routine.pdf");
    mPdfRenderer = new PdfRenderer(ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY));
}

这是错误堆栈,应用程序强制关闭

08-24 23:49:27.826    7469-7469/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.android.pdfrendererbasic, PID: 7469
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pdfrendererbasic/com.example.android.pdfrendererbasic.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.pdf.PdfRenderer$Page android.graphics.pdf.PdfRenderer.openPage(int)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2357)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            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:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.pdf.PdfRenderer$Page android.graphics.pdf.PdfRenderer.openPage(int)' on a null object reference
            at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.showPage(PdfRendererBasicFragment.java:174)
            at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.onViewCreated(PdfRendererBasicFragment.java:102)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
            at android.app.BackStackRecord.run(BackStackRecord.java:834)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
            at android.app.Activity.performStart(Activity.java:6005)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            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:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
08-24 23:49:34.928    7469-7484/com.example.android.pdfrendererbasic E/System﹕ Uncaught exception thrown by finalizer
08-24 23:49:34.928    7469-7484/com.example.android.pdfrendererbasic E/System﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void dalvik.system.CloseGuard.warnIfOpen()' on a null object reference
            at android.graphics.pdf.PdfRenderer.finalize(PdfRenderer.java:210)
            at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:191)
            at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:174)
            at java.lang.Thread.run(Thread.java:818)

0 个答案:

没有答案