1

I have a html string containing seveal img tags which I am passing to webview's loadDataWithBaseURL method like

String data = "some html with <img> and <link>.....";
wview.loadDataWithBaseURL("http://dummy.baseurl/", data, "text/html", "UTF-8", null);

if I dont pass the first parameter html can be displayed but subsequent requests for or css files are not triggered thats why I am passing a dummy baseUrl.

Running the code when I try to look what requests were made under shouldInterceptRequest() like below

wview.setWebViewClient(new WebViewClient() {

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view,
                        String url) {
            Log.d("url="+url, "resources");
            ....

        }
});

the I can see outputs like

http://dummy.host.name/images/face.jpg etc

but my original html contains ".." in img src like <img src="../images/face.jpg"> trouble is parent directory (..) part is ignored by webview

this ".." is important for me I cannot figure out why it is skipping that part

EDIT

I am loading images from a zip file so inside shouldInterceptRequest() I can put necessary logic but first I should have correct src.

I also noticed that if path is appended to baseUrl, they are also ignored for example

http://dummy.baseUrl/one/two/

becomes (looking from request logs)

http://dummy.baseurl/

I suspect if these two are related !

duckduckgo
  • 1,280
  • 1
  • 18
  • 32

2 Answers2

0

Where do you want to load the images from? If you're trying to load them from the phone you need to use the assets folder.

https://stackoverflow.com/a/7268695/642161

Community
  • 1
  • 1
dmon
  • 30,048
  • 8
  • 87
  • 96
0

I resolved my problem using a hack, not sure a better solution exist!

It looks like baseUrl and src are merged to create final request urls so if any ".." present in src it will be used to recalculate final url and they disappear no matter if top level directory exist.

I changed baseUrl to

file:///android_asset/x123_/x123_/x123_/x123_/

Here "x123_" is any random character sequence one can take but should be least likely to appear in "src"

Now count the mumber of "x123_" in request url inside shouldInterceptRequest() if it is 4 (as in this example) there were no ".." in src otherwise number of ".." = 4 - count

duckduckgo
  • 1,280
  • 1
  • 18
  • 32