voghDev / PdfViewPager

Android widget that can render PDF documents stored on SD card, linked as assets, or downloaded from a remote URL.
Apache License 2.0
1.69k stars 331 forks source link

cannot load page error #183

Open elviss116 opened 3 years ago

elviss116 commented 3 years ago

hi, i have problem when load some pdfs

i have recyclerview with multiple viewtypes, when i have 5 pdf view, the app crashed

java.lang.IllegalStateException: cannot load page at android.graphics.pdf.PdfRenderer.nativeOpenPageAndGetSize(Native Method) at android.graphics.pdf.PdfRenderer.access$300(PdfRenderer.java:105) at android.graphics.pdf.PdfRenderer$Page.(PdfRenderer.java:312) at android.graphics.pdf.PdfRenderer$Page.(PdfRenderer.java:289) at android.graphics.pdf.PdfRenderer.openPage(PdfRenderer.java:230) at es.voghdev.pdfviewpager.library.adapter.BasePDFPagerAdapter.getPDFPage(BasePDFPagerAdapter.java:162) at es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter.instantiateItem(PDFPagerAdapter.java:56) at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010) at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1224) at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092) at androidx.viewpager.widget.ViewPager$3.run(ViewPager.java:273) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1031) at android.view.Choreographer.doCallbacks(Choreographer.java:854) at android.view.Choreographer.doFrame(Choreographer.java:785) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1016) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:224) at android.app.ActivityThread.main(ActivityThread.java:7562) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

this is my android code

class ReleasePdfViewHolder(binding: ItemPublishReleasePdfBinding) : BaseViewHolder(binding), DownloadFile.Listener {

private lateinit var remotePDFViewPager: RemotePDFViewPager
private lateinit var adapterPdf: PDFPagerAdapter
private var listenerPdf : DownloadFile.Listener = this

fun bind(item: PublishView, listener: (publish: PublishView, viewType: PublishAdapter.InnerView) -> Unit){
    binding.publish = item
    binding.executePendingBindings()

    if (item.file_type.equals("C", ignoreCase = true) && item.generic.equals("S", ignoreCase = true) && item.subtypePub.equals("P", ignoreCase = true)){
        println("### LANZAR HOLDER VIEW PDF")
        remotePDFViewPager = RemotePDFViewPager(binding.root.context,item.firstUrlImg,listenerPdf)
    }
}

//TODO: CONFIG LAYOUT PDF
private fun setLayoutToPdf(){
    binding.layoutPdfRoot.removeAllViews()
    binding.layoutPdfRoot.addView(remotePDFViewPager)
}

override fun onSuccess(url: String?, destinationPath: String?) {
    println("## ON SUCCESS PDF RUN")
    adapterPdf = PDFPagerAdapter(binding.root.context, FileUtil.extractFileNameFromURL(url))
    remotePDFViewPager.adapter = adapterPdf
    setLayoutToPdf()
}

override fun onFailure(e: Exception?) {
    e?.let {
        it.printStackTrace()
    }
}

override fun onProgressUpdate(progress: Int, total: Int) {

}

}

voghDev commented 3 years ago

Hi!

it seems you have a RecyclerView with Pdf documents on each row. This can potentially lead to memory issues, as some Pdf documents are quite heavy to load in memory. If you load/unload them as the user scrolls, you may get OutOfMemory exceptions.

Anyway, it seems the PdfRenderer original component is failing to open one of your Pdfs.

elviss116 commented 3 years ago

when i load one by one , the pdf open perfect. one way to prevent the app crashing is add a try catch in PDFPageAdapter

try { PdfRenderer.Page page = getPDFPage(renderer, position); Bitmap bitmap = bitmapContainer.get(position); ssiv.setImage(ImageSource.bitmap(bitmap));

        ssiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageClickListener.onClick(v);
            }
        });

        page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
        page.close();

        container.addView(v, 0);
    }catch (Exception e){
        System.out.println("### ERROR ### ");
    }

``