Android Question [Solved] Webview local html + css + js + images

GabrielM

Member
Licensed User
What I am trying to achieve:

- download a zip file from a known URL
- that zip file contains a html file + js + css + images (all files in the same folder)
- I will then unzip the archive in : rp.GetSafeDirDefaultExternal("MYPAGE") or File.DirInternal
- then I would like to open/show that html file, probably in a Webview.

If I understood right, on the SDK30 the Webview and LoadUrl("file://"...) is not usable anymore, and also most searches I did on subject shown that the html will load with related items if they are placed in Assets folder only ?

My trouble is that the html and its related files are not known at compile time.

I have tried with : Webview.loadhtml( File.readstring( File.DirInternal, "index.htm" ))
and when loading it shows just the non-formatted html (no any images loaded, no js or css applied).

Any other way to locally load such html in application (not external browser) ?
 

HAH

Well-Known Member
Licensed User
For related files (css, js, images) you need to modify html file to point to them correctly.
Example:
B4X:
WebV.LoadHtml("<html><body background='" & WebViewAssetFile("image.jpg") & "'></body></html>")


Sub WebViewAssetFile (FileName As String) As String
   Dim jo As JavaObject
   jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
   If jo.GetField("virtualAssetsFolder") = Null Then
     Return "file:///android_asset/" & FileName.ToLowerCase
   Else
     Return "file://" & File.Combine(jo.GetField("virtualAssetsFolder"), _
       jo.RunMethod("getUnpackedVirtualAssetFile", Array As Object(FileName)))
   End If
End Sub
 
Upvote 0

GabrielM

Member
Licensed User
Sounds interesting HAH, thanks

it seems I will have to parse every single line of that html and edit it.
I will give it a try, though, see how it goes.

Sorry my noob questions:
- how the WebViewAssetFile will know from where to get the "image.jpg" file ? particularly what is the folder you are targeting in your snippet please ?
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
If I understood right, on the SDK30 the Webview and LoadUrl("file://"...) is not usable anymore,

this is not quite correct. you need to turn a webview setting on to
continue to allow file:// url's. the websettings library has the method.
(google turned the setting to off by default as of api30. it was on before.)

this is not what google prefers, but it is still not deprecated. i'm not
recommending it, but it would still allow you to store your downloaded
files in dirinternal or safedirdefaultexternal (both tested, along with dirassets).
 
Upvote 0

GabrielM

Member
Licensed User
I see, so it is not deprecated yet, I must got it wrong, thank you @drgottjr
Probably I should later adopt another approach, something like a fixed layout and only update the images and text for elements in it.
 
Upvote 0
Top