I have a WebView in my application in which some site is opened (always the same, it is my own page). The site has some JS code which loads some images from the remote host.
I want to intercept requests to such images (by URL pattern) and give back my own content (i.e. another image), or leave request untouched depending on internal application logic.
Is it possible to do that?
EDIT: The current state of question...
WebView
has an ability to set a WebViewClient
(as noted by Axarydax). WebViewClient
have two useful methods
shouldOverrideUrlLoading
onLoadResource
shouldOverrideUrlLoading
is able to intercept any url loading, if loading is triggered by page interaction (i.e. link on a page is clicked, WebView.loadURL("") isn't triggering this method). It is also able to cancel URL loading by returning false. This approach isn't usable, cause' it is not able to intercept loading of page resources (and images, what i need to intercept, is such a page resource).
onLoadResource
is triggering every time that page resource (and images! thx to jessyjones) are loading, but there is no way to cancel that. That makes this method not suitable for my task also.
Here is my solution I use for my app.
I have several asset folder with css / js / img anf font files.
The application gets all filenames and looks if a file with this name is requested. If yes, it loads it from asset folder.