Android Question [Solved]WebView cannot access local web page files in Android 10

cqzhongcheng

Member
Licensed User
hi:

i use webview to show local web page files at File.DirDefaultExternal.
the code is : webview1.LoadUrl("file:///storage/emulated/0/Android/data/pldata.check/files/web/index.html")
in Android 9 ,this code run success
but in Android 10 , failed. the fail message is "net::ERR_ACCESS_DENIED"
why?

thanks.
 

cqzhongcheng

Member
Licensed User
sorry, the code is:
B4X:
webview1.LoadUrl("file://" & File.Combine(File.DirDefaultExternal, "/web/index.html")

I guess the path obtained by file. Combine is wrong
 
Upvote 0

cqzhongcheng

Member
Licensed User
i find other article , some say the code should use: XUI.FileUri
but i can't find this function in whitch library ?
i check library XUI ,XUI Views , XUI2D , there have no this function: XUI.FileUri
 
Upvote 0

FrostCodes

Active Member
Licensed User
sorry, the code is:
B4X:
webview1.LoadUrl("file://" & File.Combine(File.DirDefaultExternal, "/web/index.html")

I guess the path obtained by file. Combine is wrong
I think Erel warned that you should never use DirDefaultExternal instead why not Use XUI to load the URL and also if you need to copy out the files, copy it using runtime permissions or copy it to the DirInternal.
 
Upvote 0

cqzhongcheng

Member
Licensed User
i change the manifest file :
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="30"/>
to
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>

then the webview show normal

But this is not the best solution. If WebView is really unavailable in the future, many parts of my app will have to be changed
Don't know if other friends have a better way?
 
Upvote 0

DonManfred

Expert
Licensed User
Upvote 0

drgottjr

Well-Known Member
Licensed User
i find this article: https://www.b4x.com/android/forum/threads/webview-access-denied-with-api-30.132424/#content
it say "Webview- Access Denied with API 30"
is it true ?
if use loadhtml function ,how can i load js and css file ?
NO, NO, NO. you totally misread the thread. it is the "file://" scheme that is not allowed. webview can load local files with api 30. several methods are available, including the new assetloader api. i provided examples in that post and elsewhere.
 
Upvote 0

cqzhongcheng

Member
Licensed User
NO, NO, NO. you totally misread the thread. it is the "file://" scheme that is not allowed. webview can load local files with api 30. several methods are available, including the new assetloader api. i provided examples in that post and elsewhere.
the first method "loadhtml" , if the html contains css file, js file and image file ,then loadhtml will not show correct

the second method "androidx.webkit.WebViewAssetLoader " , I can't use Java technology. I tried it without success , I'm waiting. B4a can give a convenient calling method

another question : if the html file put in assert directory , It is very inconvenient to reference CSS, JS and image file
Thank you for your answer !
 
Last edited:
Upvote 0

cqzhongcheng

Member
Licensed User
Don´t use Webview. Build a native UI instead.
My technology is not good. It's difficult for me to make a beautiful interface with the native UI.
HTML5 has many beautiful free frameworks .
I don't know if there is a free beautiful native b4a UI framework to use ?
Thank you for your answer!
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
the first method "loadhtml" , if the html contains css file, js file and image file ,then loadhtml will not show correct

the second method "androidx.webkit.WebViewAssetLoader " , I can't use Java technology. I tried it without success , I'm waiting. B4a can give a convenient calling method

another question : if the html file put in assert directory , It is very inconvenient to reference CSS, JS and image file
Thank you for your answer !

your first mistake is thinking that a webview is a substitute for a browser.
it isn't. android's documentation is quite clear about this. it does not
do everything that a browser does. and it often does not do what it says
it will do.

if you load your html, css, js and images correctly before calling loadhtml(),
the resulting string will be a correctly rendered webpage. (note: if there is
a feature in your webpage that is not supported by webview, the display will
not be correct.)

as for your dislike of being inconvenienced, i certainly sympathize with you,
but you've chosen the wrong tool for the job at hand.

in any case, attached please find a simple example showing that you can
have separate html text, .js, .css and image files loaded and displayed correctly
with webview.loadhtml() in android 30.
 

Attachments

  • webviewload.zip
    46.3 KB · Views: 76
Last edited:
Upvote 0

cqzhongcheng

Member
Licensed User
your first mistake is thinking that a webview is a substitute for a browser.
it isn't. android's documentation is quite clear about this. it does not
do everything that a browser does. and it often does not do what it says
it will do.

if you load your html, css, js and images correctly before calling loadhtml(),
the resulting string will be a correctly rendered webpage. (note: if there is
a feature in your webpage that is not supported by webview, the display will
not be correct.)

as for your dislike of being inconvenienced, i certainly sympathize with you,
but you've chosen the wrong tool for the job at hand.

in any case, attached please find a simple example showing that you can
have separate html text, .js, .css and image files loaded and displayed correctly
with webview.loadhtml() in android 30.

Thank you very much for your reply !
The code you gave solved the problem perfectly, thank you !
 
Upvote 0
Top