Android Question WebView, HTML

Discussion in 'Android Questions' started by kisoft, Feb 27, 2019.

  1. kisoft

    kisoft Active Member Licensed User

    Hi
    I try to open the html file (elem2.html) in webview.
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("1")
        WebView1.LoadHtml(
    $"<img src="${WebViewAssetFile( "elem2.html")}"/>"$)
    End Sub

    Sub WebViewAssetFile (FileName As StringAs 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
    I get a blank page. In which directory do I need to place the html file (elem2.html)?
    Do I do it well at all?
     
  2. johndb

    johndb Active Member Licensed User

    Try this:
    Code:
    WebView1.LoadUrl("file:///android_asset/" & "elem2.html")
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    This is a mistake.

    XUI.FileUri is the best way to get a link to a local file.

    I don't understand how an html file can be a source of an img element...
     
  4. kisoft

    kisoft Active Member Licensed User

    I understand that this is a mistake
    Code:
    WebView1.LoadHtml($"<img src="${WebViewAssetFile( "elem2.html")}"/>"$)
    How can I put this html file in the code?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    What is the content of this html file?
     
  6. kisoft

    kisoft Active Member Licensed User

    For the tests I took the content of the website.
     
  7. Lucas Eduardo

    Lucas Eduardo Member Licensed User

    I checked in an example that I created with files from the cellphone, to use within webview, example (js, css) debugging is not loading but realizes that it works fine in realise mode
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Use XUI.FileUri. It will work in all modes.

    Code:
    Dim xui As XUI
    WebView1.LoadUrl(xui.FileUri(
    File.DirAssets, "elem2.html"))
     
  9. kisoft

    kisoft Active Member Licensed User

    Thank you for help. Everything works.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice