Android Question BBCodeView - java.lang.IllegalArgumentException: width and height must be > 0

jahswant

Well-Known Member
Licensed User
Longtime User
I use this code in b4j to get the Snapshot of a BBCodeView. It works well.
B4X:
        Dim v As B4XView = BBCodeViewTicket.ForegroundImageView.Parent
        Dim B4XTicketBitmap As B4XBitmap = v.Snapshot
        Dim Out As OutputStream
        Out = File.OpenOutput(xui.DefaultFolder, "TicketPage.png", False)
        B4XTicketBitmap.WriteToStream(Out, 100, "PNG")
        Out.Close
I always fall in this exception (IllegalArgumentException) java.lang.IllegalArgumentException: width and height must be > 0 in b4a what could be the issue ?


B4X:
java.lang.IllegalArgumentException: width and height must be > 0
    at android.graphics.Bitmap.createBitmap(Bitmap.java:1197)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:1163)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:1111)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:1070)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.InitializeMutable(CanvasWrapper.java:654)
    at anywheresoftware.b4a.objects.B4XViewWrapper.Snapshot(B4XViewWrapper.java:361)
    at ca.purpos.admin.dashboardpage$ResumableSub_btnPrintTicketOrder_Click.resume(Unknown Source:1582)
    at ca.purpos.admin.dashboardpage._btnprintticketorder_click(Unknown Source:29)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7659)
    at android.view.View.performClickInternal(View.java:7636)
    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
    at android.view.View$PerformClick.run(View.java:30156)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:205)
    at android.os.Looper.loop(Looper.java:294)
    at android.app.ActivityThread.main(ActivityThread.java:8177)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
 
Last edited:
Solution
forget what I wrote, the solution is simpler. Just call the Base_Resize function, then it works.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    #If B4J
    File.MakeDir(File.DirData("BBCODE"),"DATA")
    xui.SetDataFolder("BBCODE")
    #End If 
    Root = Root1
    Root.LoadLayout("showbbticketlayout")
    TextEngine.Initialize(PanelBBCodeView)
    BBCodeViewTicket.Padding.Initialize(5dip, 5dip, 5dip, 5dip)
    BBCodeViewTicket.TextEngine = TextEngine
    BBCodeViewTicket.LazyLoading = False
    BBCodeViewTicket.Text = ComposeTicket
    BBCodeViewTicket.Base_Resize(BBCodeViewTicket.mBase.Width,BBCodeViewTicket.mBase.Height)
End Sub

Sub btnPrintTicketOrder_Click
  
    Dim iv As B4XView =...

DonManfred

Expert
Licensed User
Longtime User
what could be the issue ?
ForegroundImageView.Parent
Parent does not seem to have a width or height.
Mabe anchored and values are <0?

I never used this View so i can´t help any further.

It is always best to upload a small project showing the issue
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I am not sure if this will help, but this is how I get the bitmap from a bbcodeview

B4X:
Dim iv As ImageView = BBCV.ForegroundImageView
Dim bmp As Image = iv.GetImage

I have found that sometimes the view is not finished rendering, so you may to need to sleep a bit.

I have also used this

B4X:
            BBCodeView1.text = text
            Sleep(100)
            PageImage = Panel.snapshot  ' where Panel contains the base of the codeview (where the layout was loaded)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
This is a code example. The code works perfectly on B4J but not on other platforms.
The problem is, that the imageview width is -1 and this is not allowed.
My workaround is to set the width and height new and it seems to be working:
B4X:
    Dim iv As B4XView = BBCodeViewTicket.ForegroundImageView.Parent
    iv.Width = BBCodeViewTicket.ForegroundImageView.Width
    iv.Height = BBCodeViewTicket.ForegroundImageView.Height
    Dim bmp As B4XBitmap = iv.Snapshot
    Dim Out As OutputStream
    Dim rp As RuntimePermissions
    Out = File.OpenOutput(rp.GetSafeDirDefaultExternal("test"), "TicketPage.png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close
I have changed the path so that I can have a look at the result.


But only the developer of this library can probably say why it is width -1.
 
Last edited:
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
forget what I wrote, the solution is simpler. Just call the Base_Resize function, then it works.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    #If B4J
    File.MakeDir(File.DirData("BBCODE"),"DATA")
    xui.SetDataFolder("BBCODE")
    #End If 
    Root = Root1
    Root.LoadLayout("showbbticketlayout")
    TextEngine.Initialize(PanelBBCodeView)
    BBCodeViewTicket.Padding.Initialize(5dip, 5dip, 5dip, 5dip)
    BBCodeViewTicket.TextEngine = TextEngine
    BBCodeViewTicket.LazyLoading = False
    BBCodeViewTicket.Text = ComposeTicket
    BBCodeViewTicket.Base_Resize(BBCodeViewTicket.mBase.Width,BBCodeViewTicket.mBase.Height)
End Sub

Sub btnPrintTicketOrder_Click
  
    Dim iv As B4XView = BBCodeViewTicket.ForegroundImageView.Parent
    Dim bmp As B4XBitmap = iv.Snapshot 
    Dim Out As OutputStream
    Dim rp As RuntimePermissions
    Out = File.OpenOutput(rp.GetSafeDirDefaultExternal("test"), "TicketPage.png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close
  
End Sub

Since there is no Resize event in B4A, you have to call a Base_Resize for a custom view in the DesignerCreateView sub, this is missing in BBCodeView.bas, so you have to call it manually in B4A. or @Erel adds this line to the BBCodeView.bas.
B4X:
#If B4A
Base_Resize(mBase.Width,mBase.height)
#End If
 

Attachments

  • BBCODE.zip
    60 KB · Views: 31
Last edited:
Upvote 1
Solution
Top