Question

I am trying to display a captcha jpeg image (which is generated dynamically by a Java servlet) in my application's WebView, however, it will sometimes display a blue question mark instead of the captcha image. Below is what I have in my shouldOverrideUrlLoading(WebView view, String url) method of MyWebViewClient class that extends WebViewClient

I have internet access enabled in my manifest and the problem occurs for about 80% of the time. i.e. Sometimes the image does show up correctly but very rare though.

There was another similar question here Image with dynamic src loads in Android browser, but not in Webview, but does not seem to have a definitive answer.

Thanks in advance

try
            {
                CookieSyncManager.getInstance().sync();
                Log.v("Hello","CookieStore: " + httpClient.getCookieStore().toString());
                HttpResponse response = httpClient.execute(httppost);
                data = new BasicResponseHandler().handleResponse(response);
                view.loadDataWithBaseURL(URL.toString(), data, "text/html", "UTF-8" , null);
                //Log.v("Hello","Data: " + data.toString());
            }
            catch (ClientProtocolException e)
            {  
                Log.v("Hello","ClientProtocolException:Overriding");
                Log.v("Hello",e.toString());
                e.printStackTrace();    
            } 
            catch (IOException e) 
            {   
                Log.v("Hello","IOException:Overriding");
                Log.v("Hello",e.toString());
                e.printStackTrace();  
            }
Was it helpful?

Solution

I just got the same problem. I identified the issue and solved it (in my case). You can notice that in HTML code, the img tag has the src pointing to the relative path of the site. Something like <img src="/relativepath/captcha.jpg?12345678"/>. All you have to do is to inject the full path (host + relative path) to obtain something like <img src="http://yourhost.com/relativepath/captcha.jpg?12345678">.

  1. Read the HTML code, line by line
  2. Match the sequence "<img src" (maybe something a little bit differente in your site) with each line
  3. When you found the line you will split it until you find the link inside the src=""
  4. Reconstruct your tag with the host name on it: <img src="http://host.com" + link >

Then the image will appear ;)

OTHER TIPS

Try using view.loadUrl(URL.toString()); instead of loadDataWithBaseURL

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top