(Here is the iOS version)
This is how you can set HTML data to Label (works only in release mode),
Dependencies: StringUtils, jSoup, LabelExtras
Here is the code,
Here is the image downloader service module code save it as "HtmlViewImgDownloader",
Happy Coding
This is how you can set HTML data to Label (works only in release mode),
Dependencies: StringUtils, jSoup, LabelExtras
Here is the code,
B4X:
Dim extraHeight As Int = 0 'global variable (height of images)
Dim htmlParser As Html 'global variable
Dim jSoup As jSoup 'global variable
Dim htmlLabel As Label 'global variable
Dim strUtils As StringUtils 'global variable
Dim HtmlString = "YOUT HTML STRING"
Dim HtmlImgList As List = jSoup.selectorElementAttr(HtmlString,"img","src") 'list all the images to be downloaded
'HtmlViewImgDownloader is a replica of Erel's ImageDownloader class. But it just stores the downloaded image to cache folder of app
Dim rs As ResumableSub = CallSub2(HtmlViewImgDownloader,"Download",HtmlImgList)
Wait For (rs) Complete (result As Boolean)
htmlLabel.Initialize("")
htmlLabel.Text = htmlParser.FromHtml2(HtmlString,"ImageGetter","TagGetter")
htmlLabel.TextSize = 16
htmlLabel.width = 100%x
htmlLabel.height = strUtils.MeasureMultilineTextHeight(htmlLabel,htmlLabel.Text.Trim) + extraHeight
'It handles image tags while parsing your html string
Sub ImageGetter_GetDrawable(Source As String) As Object
Source = Regex.Split("/",Source)(Regex.Split("/",Source).Length-1) 'get image name from source string
If File.Exists(File.DirDefaultExternal &"/cache", Source) Then
Dim Bitmap1 As Bitmap
Bitmap1=LoadBitmap(File.DirDefaultExternal &"/cache", Source)
Dim BitmapDrawable1 As BitmapDrawable
BitmapDrawable1.Initialize(Bitmap1)
Dim l As LabelExtras
l.SetDrawableBounds(BitmapDrawable1, 0, 0, 100%x, 100%x * (Bitmap1.Height/Bitmap1.Width))
extraHeight = extraHeight + 100%x * (Bitmap1.Height/Bitmap1.Width)
Return BitmapDrawable1
Else
Return Null
End If
End Sub
Here is the image downloader service module code save it as "HtmlViewImgDownloader",
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
Private ongoingTasks As Map
End Sub
Sub Service_Create
ongoingTasks.Initialize
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub
Sub Service_Destroy
End Sub
Sub Download (ImageSource As List) As ResumableSub
For i = 0 To ImageSource.Size - 1
Dim link As String = ImageSource.get(i)
If ongoingTasks.ContainsKey(link) = False Then
ongoingTasks.Put(link, "")
Dim imgName As String = Regex.Split("/",link)(Regex.Split("/",link).Length-1)
If File.Exists(File.DirDefaultExternal & "/cache",imgName) And File.Size(File.DirDefaultExternal & "/cache",imgName)>0 Then
Try
' check if image file is corrupted or not
Dim x As Bitmap 'ignore
x = LoadBitmapResize(File.DirDefaultExternal& "/cache",imgName,50dip,50dip,True)
ongoingTasks.Remove(link)
Catch
Dim j As HttpJob
j.Initialize(link, Me)
j.Download(link)
wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim bmp As B4XBitmap = j.GetBitmap
Dim os As OutputStream
os = File.OpenOutput(File.DirDefaultExternal & "/cache",imgName,False)
If imgName.ToLowerCase.EndsWith(".png") Then
bmp.WriteToStream(os,100,"PNG")
Else
bmp.WriteToStream(os,100,"JPEG")
End If
os.Close
ongoingTasks.Remove(j.JobName)
End If
j.Release
End Try
Else
Dim j As HttpJob
j.Initialize(link, Me)
j.Download(link)
wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim bmp As B4XBitmap = j.GetBitmap
Dim os As OutputStream
os = File.OpenOutput(File.DirDefaultExternal & "/cache",imgName,False)
If imgName.ToLowerCase.EndsWith(".png") Then
bmp.WriteToStream(os,100,"PNG")
Else
bmp.WriteToStream(os,100,"JPEG")
End If
os.Close
ongoingTasks.Remove(j.JobName)
End If
j.Release
End If
End If
Next
Return True
End Sub
Sub ActivityIsPaused
ongoingTasks.Clear
End Sub
Happy Coding
Last edited: