Android Question ERR when loading WebView with HTML file that's in any directory except File.DirAssets

Andris

Active Member
Licensed User
Longtime User
I need to load a WebView with an HTML file, from a directory other than File.DirAssets. But in doing that, I'm always getting the following ERR_ACCESS_DENIED message within the WebView:
1731207396433.png


Here are the two pertinent lines in my code. I put simple text into info.html and place it in File.DirInternal. Then I load it into my WebView (wvEventInfo).

B4X:
    ...
    File.WriteString(File.DirInternal,"info.html","This should display in the WebView.")
    wvEventInfo.LoadUrl("file://" & File.Combine(File.DirInternal,"info.html"))
    ...

My targer SDK is 34. I'm aware that at some point, Google Android may no longer allow the LoadUrl function. Have we arrived at that point already? Otherwise, I'm stumped as to why I'm getting the error. I know I could just load the html into the WebView directly, with LoadHTML, but I can't do that because the HTML will have custom font CSS in it, which requires that the font.ttf file be in the same directory. I'd appreciate any help or comments on this.
 

teddybear

Well-Known Member
Licensed User
1.Use FIleProvider.GetFileUri to get the file url.
B4X:
    Dim p As FileProvider
    WebView1.loadurl(p.GetFileUri(File.Combine(File.DirInternal,"info.html")))
2.Or use webview.LoadHtml to read the html file
B4X:
WebView1.LoadHtml(File.ReadString(File.DirInternal,"info.html"))
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I'm aware that at some point, Google Android may no longer allow the LoadUrl function.
Where did you hear that this may happen?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is related to a change in the default value since Android 11. I will add a property to WebView. For now you need to use WebViewSettings library to change it:
B4X:
Dim settings As WebViewSettings
settings.setAllowFileAccess(WebView1, True)

 
Last edited:
Upvote 1

Andris

Active Member
Licensed User
Longtime User
Where did you hear that this may happen?
I saw it mentioned/discussed here on a forum in a couple of posts. However, I just researched it and found the best summary of webview.loadurl use as written by Grok:
Prompt: Does Google mention anywhere that webview loadurl use should be avoided?

Answer: Google does not explicitly state that `loadUrl` should be avoided in general for WebView usage. However, there are guidelines and best practices for using WebView in Android applications:

**Security Considerations**: Google advises developers to be cautious with `loadUrl` when it comes to security. For instance, when using WebView to display web content, it's recommended to use `loadUrl` with network-based URLs for optimal ad monetization and cookie functionality. However, if URLs are mishandled or if not following specific security practices like enabling Safe Browsing before calling `loadUrl`, there could be security risks. -
**Performance and Functionality**: While not avoidance, Google suggests using `loadUrl` directly with network-based URLs for better performance in terms of ad monetization and cookie handling. They also mention that for displaying static HTML content, alternatives like `loadData` might be more appropriate. -
**User Experience**: To prevent the WebView from launching external browsers when clicking links, Google advises developers to override `shouldOverrideUrlLoading` in a custom `WebViewClient`. This is not an avoidance of `loadUrl` but a practice to enhance user experience within the app. From various developer resources and forums, like Stack Overflow and Google's official documentation:
- There's emphasis on configuring WebViews correctly for security and performance, but `loadUrl` itself is not discouraged. Instead, the focus is on how it's implemented: - Implementing a `WebViewClient` to handle URL loading within the app rather than external browsers.
- Enabling JavaScript and other settings for proper functionality.
- Ensuring proper handling of cookies and security features like Safe Browsing. Therefore, while Google doesn't say to avoid `loadUrl`, they highlight the importance of using it correctly within the context of security, performance, and user experience. Here's a concise summary based on the information: -
**Use `loadUrl` with caution**: Ensure security by implementing `WebViewClient`, enabling Safe Browsing, and handling URLs properly within the app. -
**Optimal use**: For web content, especially for monetization purposes, use `loadUrl` with network URLs. -
**Consider alternatives**: For static HTML, `loadData` might be preferable over `loadUrl`. So, while `loadUrl` isn't to be avoided, its use should be managed with attention to these considerations.

Thanks for asking the question.
 
Last edited:
Upvote 0

Andris

Active Member
Licensed User
Longtime User
This is related to a change in the default value since Android 11. I will add a property to WebView. For now you need to use WebViewSettings library to change it:
B4X:
Dim settings As WebViewSettings
settings.setAllowFileAccess(WebView1, True)

Can't believe the "new library" is alredy 13 years old! Thanks for all you've done to keep this amazing platform evolving.
 
Upvote 0
Top