Java Question event not raising

DonManfred

Expert
Licensed User
Longtime User
i have a javalibrarywrapper where i want to raise a event in b4a

B4X:
        final ParcelFileDescriptor pfd = openFile(dir,filename);
        try {
            PdfRenderer renderer = new PdfRenderer(pfd);
            app.Log("renderer");                               
            // let us just render all pages
            final int pageCount = renderer.getPageCount();
            app.Log("pagecound "+pageCount);                               
            for (int i = 0; i < pageCount; i++) {
                Page page = renderer.openPage(i);
                app.Log("page open "+i);                               
               
                int w = page.getWidth();
                int h = page.getHeight();
               
                // say we render for showing on the screen
            Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
            Bitmap mBitmap = Bitmap.createBitmap(w, h, conf);
           
            Rect r = new Rect(0, 0, w, h);
           
                page.render(mBitmap, r, null, page.RENDER_MODE_FOR_PRINT); // .RENDER_MODE_FOR_DISPLAY);
                // do stuff with the bitmap
                String file_path = destpath;
                File dir1 = new File(file_path);
                if(!dir1.exists())
                    dir1.mkdirs();
                File file = new File(dir1, filename+"-" + i + ".png");
                FileOutputStream fOut = new FileOutputStream(file);
                mBitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
                fOut.flush();
                fOut.close();
                app.Log(filename+"-" + i + ".png written to "+file_path);                               
               
                if (app.subExists(eventName + "_pagerendered")) {
                    app.Log("Raising.. "+eventName + "_pagerendered()");                               
                    app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, mBitmap);
                }else {
                    BA.Log("NOTFOUND '"+eventName + "_pagerendered");
                }
                app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, mBitmap); //
                // close the page
                page.close();
            }
            renderer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

For testing i put code to write the bitmap directly from the java-library... This works; the files are written here...

the sub in b4a do exists and seems to have the right signature

B4X:
 if (app.subExists(eventName + "_pagerendered")) {
 app.Log("Raising.. "+eventName + "_pagerendered()");
The log Raising... appears in b4as log
but the event do not raise. Why?

B4X:
@Events(values={"pagerendered (page As Int, pagecount As Int, image As Bitmap)"})
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    File.Copy(File.DirAssets,"pdfformularbeispiel.pdf",File.DirRootExternal,"pdfformularbeispiel.pdf")
    pdfr.Initialize("pdf",File.DirRootExternal,"pdfformularbeispiel.pdf", File.DirRootExternal&"/PDFs")
End Sub
Sub pdf_pagerendered (page As Int, pagecount As Int, image As Bitmap)
    Log("b4a pagerendered ("&page&","&pagecount&")")
    Dim bdw As BitmapDrawable
    bdw.Initialize(image)
    Activity.Background = bdw
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Thank you for your answer, @thedesolatesoulReally appreciated

BTW: The wrapper is a wrapper for

i already do this (in a part of code i did not post)...

And i have found the problem.

to test the library i quickly called the initialization from activity_create.... but in this initialize the complete work are done. Including raising the events....

If i move the initialize to activity_resume then all works fine.

I´ll split init and the working-code into 2 library-calls

Thread can be closed

PS: Here is one resulting image
b4acodesnippets.pdf-5.png


And here another one with graphics on it...

guide.pdf-16.png
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
the library - whwn i´m finished - should be able to return any page from the pdf as bitmap
You should be able to easily build your own viewer with the lib within an event
B4X:
Sub pdf_pagerendered (page As Int, pagecount As Int, image As Bitmap)
    Log("b4a pagerendered ("&page&","&pagecount&")")
End Sub
Do whatever you want in this event with the bitmap :)
As you can see here
B4X:
app.Log("pagecound "+pageCount);                             
            for (int i = 0; i < pageCount; i++) {
                Page page = renderer.openPage(i);
                app.Log("page open "+i);
it is actually rendering all pages from the pdf.... And as you can imagine... i can make methods to display just paxe x, y, y to y whatever....

I did a test a few minutes ago and have rendered the users guide (328 pages) into PNGs... Now i have 328 single-page-images from he pdf ;)
 
Last edited:

warwound

Expert
Licensed User
Longtime User
It might be wise to run the code that renders the PNGs in a background thread anyway.
Even if you're only rendering 1 or a few pages it's a possibly processor hungry operation.
Rendering relatively simple PDF pages might work ok now, but further down the line you might start rendering more complex PDF pages and then face the 'Force Close' dialog.

Read about the submitRunnable method.
 

DonManfred

Expert
Licensed User
Longtime User
Read about the submitRunnable method.

Thanks for the hint!

A quick question ;-)

if i initialize a pdfrenderer
B4X:
mPdfRenderer = new PdfRenderer(pfd);
and store this is a class-variable.. Then i just use the renderer methods to get the pagecount, render only page x...

Can i use the mPdfRenderer in a runable or do i need to create a new instance in the runable?
 

warwound

Expert
Licensed User
Longtime User
Thanks for the hint!

A quick question ;-)

if i initialize a pdfrenderer
B4X:
mPdfRenderer = new PdfRenderer(pfd);
and store this is a class-variable.. Then i just use the renderer methods to get the pagecount, render only page x...

Can i use the mPdfRenderer in a runable or do i need to create a new instance in the runable?

Your Runnable will be able to access all class members so there should be no need to create a new instance within the Runnable.
 

DonManfred

Expert
Licensed User
Longtime User
Top