AndroidPDFViewerLibrary icon indicating copy to clipboard operation
AndroidPDFViewerLibrary copied to clipboard

Problem rendering in PDFPage.getImage

Open FranciscoBartilotti opened this issue 9 years ago • 0 comments

First of all, thank you for providing this library!

I call mPDFPage.getImage(width,height, rectF, true, true); inside an async task, and in my older test device (Galaxy SIII Mini, Android 4.1.2) it works, but in my new test device (Moto G3, Android 6.0) there is about a 50% chance that the bitmap will be created with the correct size, but either completely white or black.

I have been debugging and traced the problem to:

/**
     * create a new PDFGraphics state
     * @param page the current page
     * @param imageInfo the parameters of the image to render
     */
    public PDFRenderer(PDFPage page, ImageInfo imageInfo, Bitmap bi)
    {
        super();

        this.page = page;
        this.imageinfo = imageInfo;
        this.imageRef = new WeakReference<BiCa>(new BiCa(bi, g));

//        // initialize the list of observers
//        observers = new ArrayList<ImageObserver>();
        this.cmdCnt = 0;
    }

where the WeakReference is null, thus finalizing the iterate() method. Why this happens is still unknown to me.

As a workaround, I setted HardReference.sKeepCaches to true in my app, but still a HardReference object was never created.

Later, I noticed that setting HardReference.sKeepCaches in my app, changed HardReference.java, while the WeakReferences where created in WeakReference.class

Next, I added these methods to PDFFile:

public static void clearHardRefs(){
        HardReference.cleanup();
    }
public static void setKeepCaches(boolean keep){
        HardReference.sKeepCaches=keep;
    }

And succeeded, now the references are kept in the static ArrayList of HardReference and when no longer needed, removed, and the bitmap is now 100% correct so far in both devices.

FranciscoBartilotti avatar Apr 26 '16 20:04 FranciscoBartilotti