Android Question Printing via intent share to a local networked printer

Andrew4Good

Member
Licensed User
Hello,

I'm trying to print a webpage to a local printer via intent. (wifi ofcourse)
This webpage is created from records in a SQLITe database.
To print, I use Intent Action_Sent.
For pages smaller then, let's say 85Kb, this works nicely.
However above the 85k either my program crashes and disappears from the screen or it does not show the app's which can be used to share the page (to my print app) when I press the share button in my program.
When debugging I get no errorr.
Perhaps this is some buffer issue?

My code snippet:
B4X:
Dim Intent1 As Intent
Intent1.Initialize(Intent1.ACTION_SEND, "")
Intent1.SetType("text/html")
Intent1.PutExtra("android.intent.extra.TEXT", edtReportingHtmlText.Text)
Intent1.WrapAsIntentChooser("Choose")
StartActivity(Intent1)

I'm printing the content of an edittext (html layout) which is created during quering the SQLITE database.
When finished, the Webview is filled with the content of the EditText content.
I also create an HTML file so it can be reloaded if the user want's the same page again.
(So perhaps even a html file print form my program can be an option)

I also tried streaming a file, but can't seem to get anything streamed, so still 'no cigar'

I've tried to find the options etc for intent on B4A, but can't seem to find anything sensible about it.
Searching the web, there are examples for Java.
But as i'm starting in B4A and not being a Java programmer, I don't know how to convert the intent syntax from Java to B4a.

This is why I whent for B4A ofcourse :)

Can anybody provide some solution?

Thanks
 

Andrew4Good

Member
Licensed User
Can you post the error message you get when the app crashes?
Thanks for your reply Erel,

Wel, as said, I get no error messages what so ever, not when i'm debugging nor when i'm just running it as a release version.

I've tested it a little bit more and when the HTML file is 84,5 kb then everything works like expected.
But at 85.4kb the html shows in the webview but when touching the share button, the program just stops, and i'm back on the andoid home screen (of the device) as said no errors

I also tried removing the line:
Intent1.WrapAsIntentChooser("Choose")

This gives in a better result and I can print pages upto 426 kb
But above the 426kb, when pressing the share button, nothing happens at all.
the webpage is still on the screen, stepping through it with the debugger, shows that all lines are visitted, but nothing happens, no popup of apps for sharing and here also no errors..

just a thought: is there a way to hide some types errors which I must activate to show them?

i'm stumped :(

Update:
I have created a new little program only with a edtittext and a button for loading the html file and in this button placed the intent code (see first post), after pressing the button the problem remains, Either crashing return to the android home screen, or showing no Share popup and so doing nothing at all..
Code of the small program:

B4X:
Sub Globals
    Private btnPrintingShare As Button
    Private edtReportingHtmlText As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    DateTime.DateFormat="dd/MM/yy"
    Activity.LoadLayout("Main")
End Sub

Sub btnPrintingShare_Click
        Dim fs As String = "ExpensesList_" & DateTimeTicksToFileNameString(DateTime.Now,"-") & ".html"
        edtReportingHtmlText.Text = File.ReadString(File.DirDefaultExternal,"Reports/" & fs)
        Dim Intent1 As Intent
        Intent1.Initialize(Intent1.ACTION_SEND, "")
          Intent1.SetType("text/html")
        Intent1.PutExtra("android.intent.extra.TEXT", edtReportingHtmlText.Text)
        'Intent1.WrapAsIntentChooser("Choose")
        StartActivity(Intent1)
End Sub

Sub DateTimeTicksToFileNameString(DT As Long,Devider As String) As String 'Converts Ticks to a date filename
    Dim DTTF As String  = DateTime.Date(DT)
    DTTF = DTTF.Replace("/",Devider)
    Return DTTF
End Sub

Nothing special i would say
 
Last edited:
Upvote 0

Andrew4Good

Member
Licensed User
You should check the unfiltered logs, there might be an error there.

Do you want to send this intent to a specific app? Maybe you can send a file uri instead?

Where can i find these unfiltered logs?

I want to send it to an printing app, which will print the page to my wifi connected printer.
(and sending via email is also an option ofcourse)
if I would produce this program, it must be able to use other apps also.

But your suggestion, printing the file, might result in a solution to this problem.
I have no concerns, printing the file instead of the edittext view content.

But as Said in the first Post, I can't find anything about 'Intent' for basci4android :(
Could you provide me with a small snippet example to print a html file? (and perhaps some information/Url for using intent on b4a)

Update:

Tried the Print a file suggestion, and found an example in:
http://www.basic4ppc.com/android/forum/threads/wireless-printing-in-b4a.25780/

And finally found a solution through that example:
B4X:
Dim fs As String = "/Reports/ExpensesList_" & DateTimeTicksToFileNameString(DateTime.Now,"-") & ".html"
Dim intent1 As Intent
intent1.Initialize(intent1.ACTION_VIEW, "file://" & File.DirDefaultExternal & fs)
intent1.SetType("text/html")
intent1.WrapAsIntentChooser("Send To")
StartActivity(intent1)

At first I still used ACTION_SEND, this went nowhere, and after changing it to ACTION_VIEW, my Print app displays/prints ALL records perfectly. I can send it to the Firefox and dolphin (not to chrome nor email however)

It's a pitty the documentation for intent at http://www.basic4ppc.com/android/wiki/index.php/Intent is very shalow.
Some extra explanation would be welcome, like which options there are and perhaps some examples.
Hopefully someone with a lot of knowledge on Intent will add it someday.

Thanks for your support Erel. :)
 
Last edited:
Upvote 0
Top