googlearchive / android-PdfRendererBasic

Migrated:
https://github.com/android/graphics
Apache License 2.0
518 stars 155 forks source link

Not create document . Error #1

Closed farhatmo closed 9 years ago

farhatmo commented 9 years ago

Hello,

The sample is working as expected but when i add a permission on the manifest file like INTERNET permission The pdf can't be open with a stack of error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{exemple.com.android.appliSample/exemple.com.android.appliSample.activity.PdfReaderActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:5698) 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:1028) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference at exemple.com.android.appliSample.fragment.PdfRendererBasicFragment.showPage(PdfRendererBasicFragment.java:177) at exemple.com.android.appliSample.fragment.PdfRendererBasicFragment.onViewCreated(PdfRendererBasicFragment.java:109) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116) at android.app.BackStackRecord.run(BackStackRecord.java:836) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1581) at android.app.Activity.performStart(Activity.java:5974) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2375)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:158)             at android.app.ActivityThread.main(ActivityThread.java:5698)             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:1028)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

What i need to add ?

jifffffy commented 9 years ago

Hi, I have the same problem: java.io.IOException: not create document. Error: at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method) at android.graphics.pdf.PdfRenderer.(PdfRenderer.java:153) at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.openRenderer(PdfRendererBasicFragment.java:141)

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference
        at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.showPage(PdfRendererBasicFragment.java:163)
        at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.onViewCreated(PdfRendererBasicFragment.java:101)
        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:2288)

            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)             at android.app.ActivityThread.access$800(ActivityThread.java:151)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5254)             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:903)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

the demo is working, I only change the sample.pdf to my own pdf.

yaraki commented 9 years ago

That error means that mPdfRenderer = new PdfRenderer(mFileDescriptor) in the openRenderer method was not called, which will not happen just by adding some permissions or replacing the PDF file. What else have you changed? Did you change the logic around openRenderer? Feel free to reopen the issue if this doesn't work.

jadiaheno commented 8 years ago

i have the exact same problem

namealperalp commented 8 years ago

Same problem w aide ide

joselitofilho commented 8 years ago

I have the same problem.

gunjot-mansa commented 8 years ago

Any solution to the issue?

swetakadam commented 8 years ago

any solution still the same ... I have just replaced file ... not changed any other code

swetakadam commented 8 years ago

@yaraki -- it is not working please can we open this again

yaraki commented 8 years ago

Can you provide a project to reproduce the issue?

swetakadam commented 8 years ago

@yaraki -- I solved the issue for me ... .. check this discussion they provide deeper details https://code.google.com/p/android/issues/detail?id=82841 ... sorry could not give example project ... even if you checkout google sample .. and replace sample.pdf to some other file in assets .... it will keep displaying sample.pdf .. or sometimes keep throwing above error ..

My fix was --

I am not reading from assets ... assets directory is causing issues SDcard -- files on it work as a charm ..

Another important change done by me is -- the code from onDetach is moved to onDestroy ...

I will investigate further about assets directory .. for now I wasted a day and half on this example just to change the file

yaraki commented 8 years ago

As README.md says, if you are using a file from assets, include this in your build.gradle.

android {
    aaptOptions {
        noCompress "pdf"
    }
}
swetakadam commented 8 years ago

I did that too .. as filed from assets are compressed ...

wangchauyan commented 8 years ago

@swetakadam I think you might upgrade gradle version to 2.2.0, when I upgrade gradle version to 2.2.0, then this crash will happen. So, you should keep gradle version as 2.1.2.

@yaraki I also create a PR, please have a look

yaraki commented 8 years ago

Chauyan, thanks for the report. It seems that Android gradle plugin 2.2.0 has a bug with aaptOptions: https://code.google.com/p/android/issues/detail?id=224273

wangchauyan commented 8 years ago

@yaraki , thank you for reporting.

wei-spring commented 8 years ago

hi ,you can also change .pdf file from assets to res/raw ,then everything is ok. change code : mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor(); to Resources res = context.getResources(); mFileDescriptor = res.openRawResourceFd(R.raw.sample).getParcelFileDescriptor();

vijaypwr61 commented 7 years ago

@yaraki I m using xamarin studio so in xamarin studio it is app.csprj file I tried that "" but error says "Java.io.extension: can not create file error 3"

I searched lot but couldnt find answer

vijaypwr61 commented 7 years ago

@hpu-spring87

I liked your trick so when I was trying it into my project my xamarin studio extenion for visual studio 2015 crashed so couldnt tried. I ll try

vijaypwr61 commented 7 years ago

@hpu-spring87 I tried your logic but same erro Java.IO.IOException: cannot create document. Error: 3

vijaypwr61 commented 7 years ago

I found answer for my problem here the 4th Comment from "This allows the sample to work: "