Android Question How to load image from File.DirAsset subfolder on B4A version 11

cenyu

Active Member
Licensed User
Longtime User
Hello friends, i have an old b4a app witch read html file from File.DirAssets with tihs code and display it on WebView:
B4X:
     Activity.LoadLayout("home")
   
    html = File.ReadString(File.DirAssets , "about.html")
    WebView1.Initialize("WebView1")
    Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
    WebView1.JavaScriptEnabled = True
    WebView1.ZoomEnabled=False
   
'    html = html.Replace("__BOOK_TITLE__",appBookName)
'    html = html.Replace("__BOOK_DESCRIPTION__",appBookDescription)
    WebView1.LoadHtml(html)



Into about.html i have this code:
HTML:
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body background='file:///android_asset/images/image2.jpg'>
    <!-- page content -->
    some text here
 <img src="file:///android_asset/images/image.jpg" alt="Smiley face" height="542" width="542">
    another text here
  </body>
</html>


BUT! Yesterday i install clear copy of Windows 10 64 bit. Then I install everithing for B4A acording https://www.b4x.com/b4a.html.
Since then i cant display images from about.html. file:///android_asset/ is not work....

On my other pc with B4A ver. 10 everithing works perfect

I check - the problem is not in Jdk 11. Everithin else is the same. Can anybody give me a clue?
I attach a little project witch not work on b4a ver. 11
 

Attachments

  • htmlfile.zip
    233 KB · Views: 221
Last edited:
Solution
The problem is in mapping the DirAsset folder. I suggest you copy files and folders from DirAsset to the shared folder using the file provider and then modify the HTML file and enter a new destination for the files and folders. I repeat, the problem is in mapping DirAsset for SDK30 and B4A11.

Second, you are using an older version of the WebViewExtra library. I'll give you a suggestion, download the library and B4A example from the link below. In B4AExample Activity_Create, copy all folders and files from DirAsset to the Shared folder. Before that, change the paths to the files in the HTML file to the paths for the shared folder. When you do all this, use the LoadFileFromDirAsset method to open the HTML file, do not use LoadHTML or...

agraham

Expert
Licensed User
Longtime User
Your project seems to work fine with B4A 11.20 on a Pixel 4a with Android 12 and a Redmin Note 9 Pro with Android 11. Both with targetSdkVersion of 26 as you posted and also when set to 30 as required by the Play Store. However your are not loading the html from a sub-folder as indicated in your title so I don't know what you are asking about.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
However your are not loading the html from a sub-folder as indicated in your title so I don't know what you are asking about.
The referenced images in the html are pointing to a subfolder images/
images/image.jpg and images/image2.jpg
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The referenced images in the html are pointing to a subfolder images/
Not in the project I downloaded

2022-01-18_141315.jpg


HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body background='file:///android_asset/image2.jpg'>
    <!-- page content -->
    some text here
 <img src="file:///android_asset/image.jpg" alt="Smiley face" height="542" width="542">
    another text here
  </body>
</html>
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
OK! Yes... my mistake... The html file is into Files directory in my project....Into FIles dir there are directories like images with two pictures....., CSS, js
But when i start project this pictores are not visible.... Css is not loaded...that is only when i installed B4A 11 on b4A 10 everithing work perfect...

May be i miss something... But i thing that not neded to load all dirs and files into Files manager as shown....
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
If you use on device with SDK30 (android 11), WebSetting.setAllowFileAccess i set to false by default. Try to set it to TRUE. On lower SDK's is set to true by default.
 
Upvote 1

cenyu

Active Member
Licensed User
Longtime User
I put
B4X:
WebSettings.setAllowFileAccess(WebView1,True)

but still nothing. The images into Files/Imaes directory do not shown.

I think if i put
B4X:
html = File.ReadString(File.DirAssets , "about.html")
WebView1.LoadHtml(html)
WebView1 must load entire images and css and js from FIles dir without need of puting them into File Manager
This work on b4a 10
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
The problem is in mapping the DirAsset folder. I suggest you copy files and folders from DirAsset to the shared folder using the file provider and then modify the HTML file and enter a new destination for the files and folders. I repeat, the problem is in mapping DirAsset for SDK30 and B4A11.

Second, you are using an older version of the WebViewExtra library. I'll give you a suggestion, download the library and B4A example from the link below. In B4AExample Activity_Create, copy all folders and files from DirAsset to the Shared folder. Before that, change the paths to the files in the HTML file to the paths for the shared folder. When you do all this, use the LoadFileFromDirAsset method to open the HTML file, do not use LoadHTML or LoadHTMLString.

I have been struggling for a long time in my projects with a similar problem that you have and I solved it in this way.

 
Upvote 1
Solution

cenyu

Active Member
Licensed User
Longtime User
OK!
Thank you! But how to copy files and folders from DirAsset to the shared folder
 
Last edited:
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
One file to copy from DirAsset to the shared folder is easy...But entire directory tree?
This is time consuming....Have to be other way
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
Perfect...I can not do it my self! Thanks
Here is the solution for your project. Download the project and the UltimateWebView library below. Pay attention to the settings of the UltimateWebView object in the Designer (CustomView Properties).
 

Attachments

  • UltimateWebView_2.11_Lib&Doc.rar
    112 KB · Views: 203
  • _myHtmlHelp.zip
    234.3 KB · Views: 239
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
Here is the solution for your project. Download the project and the UltimateWebView library below. Pay attention to the settings of the UltimateWebView object in the Designer (CustomView Properties).
Thanks... But this is not a solution for me.... I MUST read images from subfolder... But thanks for your help....By the way do can you help me with this question
https://www.b4x.com/android/forum/threads/how-to-copy-file-with-space-into-name.137787/

I trying to copy all files to other dir...
 
Upvote 0
Top