I want to create a WebView to display an image.
But on the second/third start of the application WebView is empty on real devices.
When I comment out //wv.getSettings().setJavaScriptEnabled(true); then all is good and when in .http I am deleting onload="resize(this);" an app is starting with image every time too.
How to correct this. How to use JavaScript and don’t to have empty WebView?
Maybe I need to save the cache after first initialisation?
public class TestwebViewimageActivity extends Activity {
private WebView wv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new HelloWebViewClient());
wv.setBackgroundColor(0x00000000);
wv.getSettings().setBuiltInZoomControls(false);
wv.setVerticalScrollBarEnabled(false);
wv.setHorizontalScrollBarEnabled(false);
wv.loadUrl("file:///android_asset/4.html");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
my .html mast show image in full window:
<html><head>
</head>
<body>
<img src="myimage.png" onload="resize(this);" />
<script type="text/javascript">
function resize(image) {
image.style['height'] = document.body.clientHeight + 'px';
image.style['width'] = document.body.clientWidth + 'px';
image.style['margin'] = 0;
document.body.style['margin'] = 0;
}
</script>
</body>
</html>
I can’t help thinking you’d be better off using CSS than JavaScript to resize your image.
I made an example using the great background-size: contain; property that auto-scales the background so it fits within the div, I think this is what you were trying to achieve with your JavaScript.
http://jsfiddle.net/4DNsL/