1

I have WebView with images, all works fine, but if i disable network connection. Open my webview with images, i'll get an ungly non-image. How i can replace default "no-image" with custom image? Have somebody ideas?

i use loadData and cached file.

> String customHtml = "<html><body><h1>Hello,</h1>" +
>             "<img src=\"http://somesite.com/image.jpg\">" +
>             "</body></html>";
>     webView = (WebView) findViewById(R.id.MyWebView);
>     webView.loadData(customHtml, "text/html; charset=utf-8", "UTF-8");

ps. Sorry for my bad english.

enter image description here

2 Answers2

0

I'd suggest to check for internet connectivity before loading the webView. This is also suggested by the Android guidelines. Check out my sample:

//Check for connection
    ConnectivityManager cm =
            (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
        activeNetwork.isConnectedOrConnecting();

return isConnected;
Foo
  • 596
  • 2
  • 5
  • 21
0

Yes i found a solution. with html...

<img src="image.png" onerror="this.src='image-not-found.png';" />

this src will be..

onerror="this.src='"+file:///android_asset/res.png+"';" />

Dont forget to turn on JavaScript support in webview

    webSettings.setJavaScriptEnabled(true);