Android Question in webview no pictures appear

DALB

Active Member
Licensed User
Hello everyone,

in a little app, i try to put a web page by this way, using a webview object in the designer which name is wv1.
with an editor, I build a page I save as 'index.html'.

This page has text and pictures.
When I import in the index.html in b4a, the pictures are in a folder 'images' I create.
But, when I launch the app, the pictures don't appear, letting only a blank area.

Here is my code. Is there a mistake somewhere ?

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("hc")

    File.Copy(File.DirAssets, "index.html", File.DirInternal, "index.html")
   
    File.MakeDir(File.Dirinternal, "\images")

    If File.Exists(File.Dirinternal & "/\images","auto.png" ) = False Then
        File.Copy(File.DirAssets, "auto.png", File.Dirinternal & "/\images", "auto.png")
    End If
   
    wv1.LoadUrl("file://" & File.Combine(File.DirInternal, "index.html"))'ok but no image

End Sub
 

sorex

Expert
Licensed User
Longtime User
there's no need for copying files.

you can replace the [path] in <img src=[path]myimg.png> with

B4X:
src=src.replace("[path]",xui.FileUri(file.dirAssets,""))

and they will show up just fine on both Android & IOS.
 
Upvote 0

DALB

Active Member
Licensed User
hello thank for answering.
I saw the strange "/\" in a web page.
trying "/" it didn't work, and I would to try the "/\" thinking that it was the right way.

A bad idea after some tryings.
But now, it works.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private wv1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("hc")

File.Copy(File.DirAssets, "index.html", File.DirInternal, "index.html")
    File.MakeDir(File.Dirinternal, "images")
    'To define a External root Dir here is very important
        File.Copy(File.DirAssets, "auto.png", File.Dirinternal & "/images", "auto.png")
        File.Copy(File.DirAssets, "DALB.png", File.Dirinternal & "/images", "DALB.png")
        File.Copy(File.DirAssets, "img0001.gif", File.Dirinternal & "/images", "img0001.gif")
    wv1.LoadUrl("file://" & File.Combine(File.DirInternal, "index.html"))'ok sans image

...

end sub

index.html has the folder images beside it containing the three pictures:

dirInternal
....................---> index.html
....................---> images (with its 3 pictures)
........................................ ---> auto.png
.........................................---> DALB.png
........................................ ---> image0001.gif

Sorex, I'll try you code soon.

Thank you for you two.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
show us index.html. exactly how is the <img> tag worded. it needs to look something like this: <img src="file:///android_asset/DALB.png">
you don't need to copy the images or the html from dirassets. they can all be loaded into your webview from dirassets.

also, fyi: that "\/" thing you saw somewhere is json encoding. it's very common.
 
Upvote 0

DALB

Active Member
Licensed User
Thanks for the information drgottjr.

I don't know a day where I never learn somehing...it makes me happy !
 
Upvote 0
Top