Web View Don't Load Web Image
I have loaded a webpage to a android webview which loads an image from its url to a webpage via javascript and it gives an error the page is not loading can anyone help? [INFO:CONS
Solution 1:
In general, javascript code running in a website cannot access resources from other websites. But a javascript from a website should be able to access resources from that same website. This is called same-origin policy, and is implemented by all major browsers.
Same thing also applies to android native webview same-origin-policy-and-android-webview
You can try below methods, if your file is in your local
WebViewwv= (WebView) vw.findViewById(R.id.help_webview);
WebSettingswebSettings= wv.getSettings();
webSettings.setAllowFileAccess(true).
webSettings.setAllowFileAccessFromFileURLs(true)
webSettings.setAllowUniversalAccessFromFileURLs(true).
These methods exist in API 16 and later and are false by default, but it’s probably a good idea to make sure of the defaults.
Solution 2:
I have experienced the same error. Javascript would try to load the image, I would get the error above. After changing the method which setups the Webview settings to the code below it started to work:
privatevoidsetupSettings() {
WebSettings settings = memoryGameView.getSettings(); // memoryGameView is an instance of android.webkit.WebView
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
}
Post a Comment for "Web View Don't Load Web Image"