Android Question Print HTML file in WebView with images

Penfound

Active Member
Licensed User
Longtime User
Hi all

I have an app that I'm writing to print Lesson Plans for me. Everything is working beautifully (thanks Erel) except....

My Lesson Plan is displayed on my device and includes a .jpg logo and this is shown correctly. In order to print the LP I have to email the file to myself which works well but the image is missing. Below is the code I use to generate the header of the LP. What am I missing please?

B4X:
 sbHeader.Append("<html><body>").Append(CRLF)
 sbHeader.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
 sbHeader.Append("<table width='" & pnlWidth & "'><tr >").Append(CRLF)
 sbHeader.Append("<td style='font-size:20';>Session Plan Open Computing")
 sbHeader.Append( "</td><td style='font-size:20;text-align:right';white-space:nowrap;>")
 sbHeader.Append("<img src='File:///android_asset/ldlogo.jpg" & "' border=0/></td></tr>") 
 sbHeader.Append( "<tr><td style='font-size:20;text-align:left';><b>Course:</b> ")
 sbHeader.Append(tmpCourse)
 sbHeader.Append( "</td><td style='font-size:20;text-align:right';white-space: nowrap;><b>Date:</b> ")
 DateTime.DateFormat ="dd MMM yyyy"
 Dim ThisTime As String
 ThisTime = DateTime.Date(DateTime.Now)
 sbHeader.Append(ThisTime)
 sbHeader.Append("</td></tr>")
 sbHeader.Append( "<tr><td style='font-size:20;text-align:left';><b>Venue:</b> ")
 sbHeader.Append(tmpVenue)
 sbHeader.Append( "</td><td style='font-size:20;text-align:right';white-space: nowrap;><b>Time:</b>")
 sbHeader.Append(tmpTime)
 sbHeader.Append( "</td></tr><tr><td colspan='2' style='font-size:20;text-align:left';>" )
 sbHeader.Append( tmpReseources)
 sbHeader.Append( "</td><td></td></tr><tr><td colspan='2'style='font-size:20;text-align:left';>" )
 sbHeader.Append( tmpLiteracy)
 sbHeader.Append( "</td><td></td></tr>" )
 sbHeader.Append("</table>").Append(CRLF)
 sbHeader.Append(sb).Append(CRLF)
 sbHeader.Append("</table></body></html>")
 File.writestring(File.DirRootExternal,"SessionPlan.html",sbHeader.ToString)
 Return sbHeader.ToString

Many thanks for loking
Penfound
 

eurojam

Well-Known Member
Licensed User
Longtime User
the problem is, that the image is linked in your HTML (File:///android_asset/ldlogo.jpg), so if you mail the HTML, there will no image be present. One solution is to embedd the image encoded as Base64.
 
Upvote 0

Penfound

Active Member
Licensed User
Longtime User
Thanks eurojam I did try looking for how to do this but found no examples - have you by any chance done it :)

Penfound
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
or you can use stringutils and do it by yourself:
B4X:
Sub ImageEncodeBase64(Dir As String, Image As String) As String
    Dim su As StringUtils
    Dim in As InputStream
    in = File.OpenInput(Dir, Image)
    Dim out As OutputStream
    out.InitializeToBytesArray(1000)
    File.Copy2(in, out) '<---- This does the actual copying
    Dim data() As Byte
    data = out.ToBytesArray
    Return su.EncodeBase64(data)
End Sub
the above function I use in B4J, but B4A must be similar...
 
Upvote 0

Penfound

Active Member
Licensed User
Longtime User
Almost there but not yet working, no image display in html doc after emailing

B4X:
sbHeader.Append( "</td><td style='font-size:20;text-align:right';white-space:nowrap;>")
sbHeader.Append("<img src='data:image/jpeg;base64,data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwkHBgoJCAkLCwoMDxkQDw4ODx4WFxIZJCAmJSMgIyIoLTkwKCo2KyIjMkQyNjs9QEBAJjBGS0U+Sjk/QD3/2wBDAQsLCw8NDx0QEB09KSMpPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT3/wAARCAAqAKQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+T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KKKACiiigAooooAKKKKAPHfGev3mua1JYhXWC3mMUUCjl2BxkjuT29K7HwX4LXR0W/1BVa/YfKvUQj/4r1Nb/wDZ9n/b32n7Jb/aPLz5vljfnp1xnpWlQJIiuLeO6haKZdyGm2lnDZRFIFwCckk5Jqeio9nDm57a9xhRRRVgf//Z' />" )
sbHeader.Append( "<tr><td style='font-size:20;text-align:left';><b>Course:</b> ")
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
you still have an error in your html:
B4X:
sbHeader.Append("<img src='data:image/jpeg;base64,data:image/jpeg;base64,/9j....
it should be
B4X:
sbHeader.Append("<img src='data:image/jpeg;base64,/9j....

Can you see the difference?
 
Upvote 0

Penfound

Active Member
Licensed User
Longtime User
eurojam - thank you so much it is sorted now.

Erel: Thank for your string literals as well - something else to get my head round:)
 
Upvote 0
Top