Android Question WebView + local picture

Akwa

Member
Licensed User
Hello,
I know that this question was already posted here one or two years ago. Unfortunately, none of the proposed solutions, in this forum, and around the web, have worked.

I have statics html pages, that have some <img src="picture.png">, that I display in a webview. I have no other solution than that.

At launch, I copy all the files (html, png) in the DirInternal (I tried also DirDefaultExternal).

I set every flags I found :

B4X:
    Dim ws As WebSettings
    ws = wve.GetSettings
    ws.SetAllowContentAccess(True)
    ws.SetAllowFileAccess(True)
    ws.SetBlockNetworkImage(False)
    ws.SetBlockNetworkLoads(False)
    ws.SetLoadsImagesAutomatically(True)
    ws.SetSupportZoom(False)
    ws.SetAppCacheEnabled(True)
    
    Dim joWebview As JavaObject = WebView1
    Dim joSettings As JavaObject = joWebview.RunMethod("getSettings", Null)
    Dim r As Reflector
    r.Target = joSettings
    r.RunMethod2("setAllowUniversalAccessFromFileURLs", True, "java.lang.boolean")
    r.RunMethod2("setDomStorageEnabled", True, "java.lang.boolean")
    r.RunMethod2("setMixedContentMode", 0x00000000, "java.lang.int")
    r.RunMethod2("setAllowFileAccess", True, "java.lang.boolean")
    r.RunMethod2("setAllowFileAccessFromFileURLs", True, "java.lang.boolean")

Then I load the page :

B4X:
Dim x As XUI
    Dim url As String =  x.FileUri(File.DirInternal, "index.htm")
    WebView1.LoadUrl(url)

Links between page works, but every pictures appears as a broken icon.

Thanks.
 

drgottjr

Expert
Licensed User
Longtime User
based on your description, and making no other assumptions as to any unrevealed intentions - if any - the code below is all you need to display an image in a webview. delete and burn the rest of your code.

B4X:
    webview.Initialize("webview")
    Activity.AddView(webview, 0%x,0%y,100%x,100%y)

    Dim html As String = $"<!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>the image you requested:<br>
    <img src="file:///android_asset/b4x.jpg" width='300px'>
    </body>
    
    </html>"$
    webview.LoadHtml(html)

'     ******* OR YOU CAN DO IT THIS WAY ******
'    Dim x As XUI
'    Dim url As String =  x.FileUri(File.DirAssets, "akwa.txt")
'    webview.LoadUrl(url)


i attach an example showing the output. (both methods produce the same result)
please add b4x.jpg to your project (in your files folder).
please add akwa.txt to your project (in your files folder) if you use the second method. you can leave it as ".txt" (forum does not uploads with ".html" suffix).

use for any other purpose than as described in the original post may not work and may require a different solution.
 

Attachments

  • akwa.png
    37.8 KB · Views: 130
  • b4x.jpg
    4.2 KB · Views: 130
  • akwa.txt
    153 bytes · Views: 143
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0

Akwa

Member
Licensed User

No no Erel, it's not a problem of large picture, it's a matter of some sort of static local WebApp, to simplify the design of the page.
 
Upvote 0

Akwa

Member
Licensed User

Thanks ! At first it doesn't work. And suddenly, I discovered it works in Release Mode, but not in Debug Mode !

@Erel, is there a reason the code works in Release and not in Debug mode ?

In debug mode, I have "Impossible to load the page ....... net: ERR_ACCESS_DENIED"
 
Upvote 1
Cookies are required to use this site. You must accept them to continue using the site. Learn more…